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"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;

int main()
{
	vector<char>chs(2000);
	vector<vector<int>>real_edges(2000);
	{
		int N, M; cin >> N >> M;
		vector<string>sts;
		for (int i = 0; i < N; ++i) {
			string st; cin >> st;
			sts.push_back(st);
		}
		vector<pair<int, int>>edges;
		for (int i = 0; i < M; ++i) {
			int a, b; cin >> a >> b;
			a--; b--;
			edges.push_back(make_pair(a, b));
		}
		int id = 0;
		vector<int>ids(N);
		for (int i = 0; i < N; ++i) {
			ids[i] = id;
			for (int j = 0; j < sts[i].size() - 1; ++j) {
				real_edges[ids[i] + j].push_back(ids[i] + j + 1);
			}
			for (int j = 0; j < sts[i].size(); ++j) {
				chs[ids[i] + j] = sts[i][j];
			}
			id += sts[i].size();
		}
		ids.push_back(id);
		for (auto e : edges) {
			real_edges[ids[e.first + 1] - 1].push_back(ids[e.second]);
		}
		const int start = id;
		const int goal = start + 1;

		for (int i = 0; i < N; ++i) {
			real_edges[start].push_back(ids[i]);
			real_edges[ids[i + 1] - 1].push_back(goal);
		}
		chs.resize(goal + 1);
		real_edges.resize(goal + 1);
	}
	const int N = real_edges.size();
	vector<vector<int>>rev_edges(N);

	for (int i = 0; i < N; ++i) {
		for (auto e : real_edges[i]) {
			rev_edges[e].push_back(i);
		}
	}

	vector<vector<int>>memo(N, vector<int>(N, -1));
	const int S = N - 2;
	const int G = S + 1;
	memo[S][G]=0;
	queue<pair<int,int>>que;
	que.push(make_pair(S,G));
	int ans=0;
	while (!que.empty()) {
		const auto atop(que.front());
		que.pop();
		const int a=atop.first;
		const int b=atop.second;

		if(memo[a][b]>8000)break;
	

		for (auto next_a : real_edges[a]) {
			for (auto next_b : rev_edges[b]) {
				if (chs[next_a] == chs[next_b]) {
					if (a == next_b&&b == next_a) {
						ans=max(ans,memo[a][b]);
						if (ans > 2010) {
							ans=-1;
							break;
						}
					}
					if (next_b == next_a) {
						ans=max(ans,memo[a][b]+1);
						if (ans > 2010) {
							ans = -1;
							break;
						}
					}
					if (memo[next_a][next_b] < memo[a][b]+2) {
						memo[next_a][next_b]=memo[a][b]+2;
						que.push(make_pair(next_a,next_b));
					}
				}
			}
		}
	}
	cout<<ans<<endl;

	return 0;
}