Under construction

Still being built. Things may change

PLCS

Time limit1sMemory limit16 MB

Summary
Find the longest common subsequence of A and B that contains X, excludes Y, and has prime length, or report -1.
Level

Medium7 of 10

Topics
Dynamic programming, String, Number theory
Solved
No attempts yet

Problem

The PLCS (Prime Longest Common Subsequence) problem gives two sequences and asks for the longest common subsequence of both whose length is prime.

Given strings AA and BB, a character XX that must be included in the PLCS, and a character YY that must not be included in the PLCS, print the length of the longest common subsequence of AA and BB that contains XX, does not contain YY, and has prime length. If no such subsequence exists, print -1.

The strings AA and BB and the characters XX and YY consist of lowercase English letters. Each of AA and BB contains exactly one XX.

Input

The first line contains the string AA.

The second line contains the string BB. (1≤∣A∣,∣B∣≤50,000)(1 \le |A|, |B| \le 50,000)

The third line contains the character XX.

The fourth line contains the character YY. (X≠Y)(X \neq Y)

Output

On the first line, print the length of the longest common subsequence of AA and BB that contains XX, does not contain YY, and has prime length. If no such subsequence exists, print -1.

Examples3

  1. Example 1

    Input
    aabbcbad
    ababacda
    c
    b
    
    Expected output
    3
    
  2. Example 2

    Input
    aabaaa
    cccbcc
    b
    c
    
    Expected output
    -1
    
  3. Example 3

    Input
    aabaaa
    bca
    b
    x
    
    Expected output
    2