|시간 제한||메모리 제한||제출||정답||맞은 사람||정답 비율|
|5 초||512 MB||0||0||0||0.000%|
A palidrome craftsperson starts to work in the early morning, with the clear air allowing him to polish up his palindromes.
On this morning, he is making his pieces to submit to the International Contest for Palindrome Craftspeople.
By the way, in order to make palindromes, he uses a special dictionary which contains a set of words and a set of ordered pairs of the words. Any words and any ordered pairs of consecutive words in his palindromes must appear in the dictionary.
We already have his dictionary, so let's estimate how long a palindrome he can make.
The first line in the data set consists of two integers N (1≤N≤100) and M (0≤M≤1 000). N describes the number of words in the dictionary and M describes the number of ordered pairs of words.
The following N lines describe the words he can use. The i-th line (1-based) contains the word i, which consists of only lower-case letters and whose length is between 1 and 10, inclusive.
The following M lines describe the ordered pairs of consecutive words he can use. The j-th line (1-based) contains two integers Xj and Yj (1≤Xj,Yj≤N). Xj describes the (1-based) index of the former word in the palindrome and Yj describes that of the latter word.
Print the maximal length of the possible palindrome in a line. If he can make no palidromes, print "0". If he can make arbitrary long palindromes, print "-1".
2 2 ab ba 1 1 1 2
2 2 ab ba 1 1 2 2
2 2 ab a 1 1 1 2