Word Equalizing

No attempts yetTime limit1sMemory limit128 MB

Problem

You are given two words xx and yy, together with a series of kk words (w1,w2,,wk)(w_1, w_2, \ldots, w_k). The operation wwiw \oplus w_i appends the series word wiw_i (1ik1 \le i \le k) to the end of the word ww (concatenation): it writes wiw_i immediately after ww.

By appending series words to the ends of xx and yy (using any word as many times as you like, on either word), decide whether the two words can be made identical. If it is possible, output the minimum number of \oplus operations required; otherwise output NIE ("no" in Polish).

For example, the words abba and ab can be equalized using the series baaabad, aa, badccaa, cc: append aa and badccaa to abba, and append baaabad, then cc, then aa to ab. Both become abbaaabadccaa, using 2+3=52 + 3 = 5 operations in total.

Input

The first line contains a positive integer kk (1k401 \le k \le 40), the length of the series. The second and third lines contain the descriptions of xx and yy. The next kk lines contain the descriptions of w1,w2,,wkw_1, w_2, \ldots, w_k, one per line. Each description consists of the word's length (a natural number) and the word itself, separated by a single space. Every word consists only of lowercase letters a to z and has length at most 2,000. The total length of all given words is at most 5,000.

Output

If xx and yy can be equalized, output the minimum number of \oplus operations (a nonnegative integer). Otherwise, output NIE.