시간 제한메모리 제한제출정답맞힌 사람정답 비율
5 초 512 MB1000.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,YjN). 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".

예제 입력 1

2 2
ab
ba
1 1
1 2

예제 출력 1

4

예제 입력 2

2 2
ab
ba
1 1
2 2

예제 출력 2

0

예제 입력 3

2 2
ab
a
1 1
1 2

예제 출력 3

-1