Problem: 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.



Input

The first line in the data set consists of two integers N (1 \leq N \leq 100) and M (0 \leq M \leq 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 X_j and Y_j (1 \leq X_j, Y_j \leq N). X_j describes the (1-based) index of the former word in the palindrome and Y_j describes that of the latter word.

Output

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".

Examples

Input

2 2
ab
ba
1 1
1 2


Output

4


Input

2 2
ab
ba
1 1
2 2


Output

0


Input

2 2
ab
a
1 1
1 2


Output

-1

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)

using namespace std;

typedef vector<int> Node;
typedef vector<Node> Graph;
typedef pair<int, int> P;
typedef tuple<int, int, int, int> T;
const int INF = 1000000000;

int main(){
    int N, M;
    while(cin >> N >> M) {
        vector<string> s(N);
        REP(i, N) cin >> s[i];

        bool d[100][100] = {};
        REP(i, M) {
            int a, b;
            cin >> a >> b;
            a--; b--;
            d[a][b] = true;
        }

        vector<P> next[100][10][26];
        REP(i, N) REP(j, s[i].size()) {
            if(j == s[i].size() - 1) {
                REP(k, N) if(d[i][k]) {
                    next[i][j][ s[k][0] - 'a' ].push_back(P(k, 0));
                }
            } else {
                next[i][j][s[i][j+1] - 'a'].push_back(P(i, j+1));
            }
        }

        vector<P> nextr[100][10][26];
        REP(i, N) REP(j, s[i].size()) {
            if(j == 0) {
                REP(k, N) if(d[k][i]) {
                    nextr[i][j][ s[k][s[k].size() - 1] - 'a' ].push_back(P(k, s[k].size() - 1));
                }
            } else {
                nextr[i][j][s[i][j-1] - 'a'].push_back(P(i, j-1));
            }
        }


        int dist[100][10][100][10] = {};
        bool inque[100][10][100][10] = {};
        //REP(i, N) REP(j, s[i].size()) REP(k, N) REP(l, s[k].size()) dist[i][j][k][l] = INF;

        queue<T> que;

        REP(i, N) REP(k, N) {
            if(s[i][0] == s[k][s[k].size() - 1]) {
                que.push(T(i, 0, k, s[k].size() - 1));
                dist[i][0][k][s[k].size() - 1] = 1;
            }
        }

        int ans = 0;
        const int MAX_D = 100000 / 100;
        while(!que.empty()){
            int a, i, b, j;
            tie(a, i, b, j) = que.front();
            que.pop();
            inque[a][i][b][j] = false;

            int cdist = dist[a][i][b][j];

            if(cdist > MAX_D) continue;

            //printf("(%d, %d) (%d, %d) : %d\n", a, i, b, j, cdist);

            if(a == b && i == j) {
                ans = max(ans, 2 * cdist - 1);
            }
            bool exist = false;
            REP(c, 26) for(auto p : next[a][i][c]) if(p.first == b && p.second == j) exist = true;
            if(exist) {
                ans = max(ans, 2 * cdist);
            }

            REP(c, 26) for(auto p : next[a][i][c]) for(auto q : nextr[b][j][c]) {
                int& ndist = dist[p.first][p.second][q.first][q.second];
                bool& inq = inque[p.first][p.second][q.first][q.second];
                if(ndist < cdist + 1) {
                    ndist = cdist + 1;
                    if(!inq) {
                        que.push(T(p.first, p.second, q.first, q.second));
                        inq = true;
                    }
                }
            }
        }
        if(ans >= MAX_D + 1) ans = -1;

        cout << ans << endl;
    }
    return 0;
}