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++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
typedef vector<int> vi;
int N,M;
const int MAX_V=210000;
string s[100];
vector<int> wG[100],wGrev[100];

vector<int> G[MAX_V];
int ord[MAX_V],low[MAX_V];
bool inS[MAX_V];
stack<int> S;
int cnt;
vector<vi> scc;
int cmp[MAX_V];

vector<int> sco[MAX_V];
vector<int> cG[MAX_V],csco[MAX_V];

void vis(int v){
	ord[v]=low[v]=++cnt;
	S.push(v);
	inS[v]=true;
	for(int u:G[v]){
		if(ord[u]==0){
			vis(u);
			chmin(low[v],low[u]);
		}else if(inS[u]){
			chmin(low[v],ord[u]);
		}
	}
	if(low[v]==ord[v]){
		vector<int> vc;
		while(true){
			int w=S.top();S.pop();
			inS[w]=false;
			vc.pb(w);
			if(v==w) break;
		}
		scc.pb(vc);
	}
}
void SCC(){
	rep(i,N*N*21) if(ord[i]==0) vis(i);
	int K=scc.size();
	rep(i,K){
		for(int v:scc[i]) cmp[v]=K-1-i;
	}
	rep(i,N*N*21){
		rep(j,G[i].size()){
			int v=cmp[i];
			int u=cmp[G[i][j]],c=sco[i][j];
			cG[v].pb(u),csco[v].pb(c);
		}
	}
}


int I(int a,int b,int c){
	return a*N*21+b*21+(c+10);
}
bool ispal(string s){
	int N=s.size();
	rep(i,N/2) if(s[i]!=s[N-1-i]) return 0;
	return 1;
}
int inf=1e9;
int main(){
	cin>>N>>M;
	rep(i,N) cin>>s[i];
	rep(i,M){
		int a,b;
		cin>>a>>b;
		wG[a-1].pb(b-1);
		wGrev[b-1].pb(a-1);
	}
	rep(i,N) rep(j,N) for(int k=-10;k<=10;k++){
		int id=I(i,j,k);
		if(k>=0){	//left
			int L=k;
			if(s[i].size()>=L){
				string st=s[i].substr(0,L);
				reverse(all(st));
				for(int v:wG[j]){
					int Lv=s[v].size();
					if(L<=Lv){
						if(s[v].substr(0,L)==st) G[id].pb(I(i,v,L-Lv)),sco[id].pb(Lv);
					}else{
						if(st.substr(0,Lv)==s[v]) G[id].pb(I(i,v,L-Lv)),sco[id].pb(Lv);
					}
				}
			}
		}
		if(k<=0){	//right
			int L=-k;
			if(s[j].size()>=L){
				string st=s[j].substr(s[j].size()-L,L);
				reverse(all(st));
				for(int v:wGrev[i]){
					int Lv=s[v].size();
					if(L<=Lv){
						if(s[v].substr(Lv-L,L)==st) G[id].pb(I(v,j,Lv-L)),sco[id].pb(Lv);
					}else{
						if(st.substr(L-Lv,Lv)==s[v]) G[id].pb(I(v,j,Lv-L)),sco[id].pb(Lv);
					}
				}
			}
		}
	}
	SCC();
	int K=scc.size();

	int dp[MAX_V]={};
	//init
	rep(i,N){
		int L=s[i].size();
		for(int j=-L;j<=L;j++){
			int id=cmp[I(i,i,j)];
			if(j>=0){
				if(ispal(s[i].substr(j,L-j))) dp[id]=L;
			}else{
				int k=-j;
				if(ispal(s[i].substr(0,L-k))) dp[id]=L;
			}
		}
	}
	rep(v,K){
		if(dp[v]==0) continue;
		if(scc[K-1-v].size()>=2) dp[v]=inf;
		rep(j,cG[v].size()){
			int u=cG[v][j],ad=csco[v][j];
			chmax(dp[u],min(inf,dp[v]+ad));
		}
	}
	int ans=0;
	rep(i,N) rep(j,N){
		int id=cmp[I(i,j,0)];
		chmax(ans,dp[id]);
	}
	if(ans==inf) ans=-1;
	cout<<ans<<endl;
}