You've just graduated from high school and are ready to start your very own mad science lab. To prove your genius to the world you plan to breed a lion-goat hybrid.
All animals in existence are fundamentally described by their DNA sequence, which is a string of N upper case letters. You've managed to obtain the DNA sequences of a lion and of a goat. To bring your new creature to life, you must produce a specific DNA sequence, called the hybrid sequence.
New DNA sequences can be produced by splicing. This involves picking two DNA sequences from your lab (call these sequences S and T) and an integer x (0 < x < N), called the splicing point. Your state-of-the-art DNA photocopier will then produce a new DNA sequence which consists of the first x characters of S, followed by the last N-x characters of T. This new sequence is then stored in your lab and can be used for future splicing.
For example, say you pick the DNA sequences GOLEM and MOOSE, and take x=2. The splicing will take GO from the start of GOLEM and OSE from the end of MOOSE, and combine these to get the new DNA sequence GOOSE.
There's been a wave of blackouts recently, so to conserve power you want to minimise use of the DNA photocopier. Determine if it is possible to create the hybrid sequence, and if so the minimum number of splices required.
It is guaranteed that all three strings in the input will be different.
If it is impossible to produce the hybrid sequence, your program should output PLAN FOILED. Otherwise, your problem should output two lines. On the first line you should output SUCCESS, and on the second line you should output a single integer: the minimum number of times you must splice to produce the required sequence.
5 MOOSE GOLEM GOOSE
3 CAT DOG COW
7 ABCDEFG HIJKLMN HBJDEMN
The first sample case corresponds to the splicing example given earlier. As explained above, you can combine the start of GOLEM and the end of MOOSE to make GOOSE in a single splice.
In the second sample case, there is no sequence of splicings that can produce the sequence COW starting with the sequences CAT and DOG.
For the third sample case, one example of an optimal sequence of splices is as follows:
For all cases, 2 ≤ N ≤ 300,000.