#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>

//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <forward_list>
//
//#define cauto const auto&
//#else

//#endif

using namespace std;


typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x) 
#define debug(x) 
#endif
#define LDcout(x,n) fixed<<setprecision(n)<<x

#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;
vector<char> v1,v2;
int dp[100][100];
int loop[100][100];
pii start;
bool ok=true;
int ans=0;
//?????£?????????????§£
class SCC{
	#define ZERO(a) memset(a,0,sizeof(a))
public:
	static const int MV = 5000;
	vector<vector<int> > SC; int NV,GR[MV];
public:
	vector<int> E[MV],RE[MV],NUM; int vis[MV];
public:
	void init(int NV) {this->NV=NV;for(int i=0;i<MV;i++){ E[i].clear();RE[i].clear();}}
	void add_edge(int x,int y){ E[x].push_back(y); RE[y].push_back(x); }
	void dfs(int cu) {vis[cu]=1;for(int i=0;i<E[cu].size();i++) if(!vis[E[cu][i]]) dfs(E[cu][i]); NUM.push_back(cu);}
	void revdfs(int cu,int ind){ vis[cu]=1;GR[cu]=ind; SC[ind].push_back(cu);
		for(int i=0;i<RE[cu].size();i++) if(!vis[RE[cu][i]]) revdfs(RE[cu][i],ind);}
	void scc(){
		int c=0; SC.clear(); SC.resize(MV); NUM.clear();
		ZERO(vis); for(int i=0;i<NV;i++) if(!vis[i]) dfs(i);
		ZERO(vis); for(int i=NUM.size()-1;i>=0;i--) if(!vis[NUM[i]]){
			SC[c].clear(); revdfs(NUM[i],c); sort(SC[c].begin(),SC[c].end()); c++;
		}
		SC.resize(c);
	}
};
int rec(int x,int y,vvint &cnt1,vvint &cnt2,vvint &b1,vvint &b2,vvint &a1,vvint &a2){
	if(dp[x][y]>=0) return dp[x][y];
	int ret=0;
	bool f=false;
	rep(i,256){
		if(cnt1[x][i]&&cnt2[y][i]){
			f=true;
		}
	}
	for(int i:b1[x]){
		for(int j:b2[y]){
			if(f&&i==x&&a1[x].size()==1) continue;
			if(f&&j==y&&a2[y].size()==1) continue;
			if(i==x&&j==y) continue;
			ret=max(ret,rec(i,j,cnt1,cnt2,b1,b2,a1,a2));
		}
	}
	ret+=f;
	return dp[x][y]=ret;
}
SCC vv1,vv2;
void mainmain(){
	rep(i,100) rep(j,100) dp[i][j]=-1;
	int n,m;
	cin>>n>>m;
	v1=vector<char>(n);
	vv1.init(n);
	rep(i,n) cin>>v1[i];
	rep(i,n){
		vv1.add_edge(i,i);
	}
	rep(i,m){
		int a,b;
		cin>>a>>b;
		vv1.add_edge(a-1,b-1);
	}
	cin>>n>>m;
	v2=vector<char>(n);
	vv2.init(n);
	rep(i,n) cin>>v2[i];
	rep(i,n){
		vv2.add_edge(i,i);
	}
	rep(i,m){
		int a,b;
		cin>>a>>b;
		vv2.add_edge(a-1,b-1);
	}
	vv1.scc();
	vv2.scc();
	vvint a1=vv1.SC;
	vvint a2=vv2.SC;
	vvint cnt1(a1.size(),vector<int>(256,0));
	vvint cnt2(a2.size(),vector<int>(256,0));
	rep(i,a1.size()) rep(j,a1[i].size()) cnt1[i][v1[a1[i][j]]]++;
	rep(i,a2.size()) rep(j,a2[i].size()) cnt2[i][v2[a2[i][j]]]++;
	bool ok=true;
	rep(i,a1.size()){
		rep(j,a2.size()){
			rep(k,256){
				if(a1[i].size()>=2&&a2[j].size()>=2&&cnt1[i][k]&&cnt2[j][k]) ok=false;
			}
		}
	}
	if(!ok){
		puts("-1");
		return;
	}
	vvint b1(a1.size()),b2(a2.size());
	vint num1(v1.size()),num2(v2.size());
	rep(i,a1.size()){
		rep(j,a1[i].size()){
			num1[a1[i][j]]=i;
		}
	}
	rep(i,a2.size()){
		rep(j,a2[i].size()){
			num2[a2[i][j]]=i;
		}
	}
	rep(i,a1.size()) b1[i].PB(i);
	rep(i,a2.size()) b2[i].PB(i);
	rep(i,5000){
		for(int j:vv1.E[i]){
			if(num1[i]==num1[j]) continue;
			b1[num1[i]].PB(num1[j]);
		}
	}
	rep(i,5000){
		for(int j:vv2.E[i]){
			if(num2[i]==num2[j]) continue;
			b2[num2[i]].PB(num2[j]);
		}
	}
	int ans=0;
	rep(i,a1.size()){
		rep(j,a2.size()){
			ans=max(ans,rec(i,j,cnt1,cnt2,b1,b2,a1,a2));
		}
	}
	cout<<ans<<endl;
}


signed main() {
	ios_base::sync_with_stdio(false);
  	cout<<fixed<<setprecision(0);
    mainmain();
}

Measured memory_bytes: 2444