#include<bits/stdc++.h>
#define rep(i,a) for(int i=0;i<(int)(a);i++)
#define rrep(i,a) for(int i=(a)-1;i>=0;i--)
#define all(a) (a).begin(),(a).end()
#define chmax(a,b) (a) = max((a),(b))
#define pb push_back
#define sz size()
using namespace std;
typedef vector<int> vi;

//Strong Connected Component Decomposition
struct StronglyConnectedComponet{
  //input & auxiliary
  int V;
  vector<vi> G, rG;
  vi order;

  //output
  int sccV;
  vector<vi> sccG, group;
  vi sccID;

  StronglyConnectedComponet(const vector<vi> &graph){
    G = graph; V = G.sz; rG.resize(V);
    rep(i,V)rG[i].clear();
    rep(i,V)for(int to : G[i])rG[to].pb(i);

    vi use(V,0);
    order.clear();
    rep(i,V)if(!use[i])fdfs(i,use);

    use = vi(V,0); sccID.resize(V);
    sccV = 0;
    rrep(i,order.sz)if(!use[order[i]])rdfs(order[i],sccV++,use);

    group.resize(sccV);
    rep(i,V)group[sccID[i]].pb(i);

    sccG.resize(sccV);
    rep(i,V)for(int to : G[i]){
      if(sccID[i]!=sccID[to])sccG[sccID[i]].pb(sccID[to]);
    }
    rep(i,sccV){
      sort(all(sccG[i]));
      sccG[i].erase(unique(all(sccG[i])),sccG[i].end());
    }
  }

  void fdfs(int v, vi &use){
    use[v] = 1;
    for(int to : G[v])if(!use[to])fdfs(to,use);
    order.push_back(v);
  }

  void rdfs(int v,int k, vi &use){
    use[v] = 1; sccID[v] = k;
    for(int to : rG[v])if(!use[to])rdfs(to,k,use);
  }
};

int main(){
  int n1,m1;
  cin >> n1 >> m1;
  vector<char> c1(n1);
  rep(i,n1)cin >> c1[i];
  vector<vi> g1(n1);
  rep(i,m1){
    int a,b;
    cin >> a >> b; a--; b--;
    g1[a].pb(b);
  }
  StronglyConnectedComponet scc1(g1);

  int n2,m2;
  cin >> n2 >> m2;
  vector<char> c2(n2);
  rep(i,n2)cin >> c2[i];
  vector<vi> g2(n2);
  rep(i,m2){
    int a,b;
    cin >> a >> b; a--; b--;
    g2[a].pb(b);
  }
  StronglyConnectedComponet scc2(g2);

  set<char> loop1, loop2;
  rep(i,scc1.sccV){
    if(scc1.group[i].sz>1){
      for(int v : scc1.group[i])loop1.insert(c1[v]);
    }
  }
  rep(i,scc2.sccV){
    if(scc2.group[i].sz>1){
      for(int v : scc2.group[i])loop2.insert(c2[v]);
    }
  }
  for(int a : loop1){
    if(loop2.count(a)){
      cout << -1 << endl;
      return 0;
    }
  }

  int ans = 0;
  vector<vi> dp(scc1.sccV,vi(scc2.sccV,0));
  rep(i,scc1.sccV)rep(j,scc2.sccV){
    if(scc1.group[i].sz>1 && scc2.group[j].sz>1){
      for(int v : scc1.sccG[i])chmax(dp[v][j], dp[i][j]);
      for(int v : scc2.sccG[j])chmax(dp[i][v], dp[i][j]);
    }else if(scc1.group[i].sz>1){
      bool f = false;
      for(int a : scc1.group[i]){
	if(c2[scc2.group[j][0]] == c1[a])f = true;
      }
      
      if(f)chmax(ans,dp[i][j]+1);
      for(int v : scc2.sccG[j])chmax(dp[i][v], dp[i][j] + (f?1:0));
      if(!f)for(int v : scc1.sccG[i])chmax(dp[v][j], dp[i][j]);
    }else if(scc2.group[j].sz>1){
      bool f = false;
      for(int b : scc2.group[j]){
	if(c1[scc1.group[i][0]] == c2[b])f = true;
      }
      
      if(f)chmax(ans,dp[i][j]+1);
      for(int v : scc1.sccG[i])chmax(dp[v][j], dp[i][j] + (f?1:0));
      if(!f)for(int v : scc2.sccG[j])chmax(dp[i][v], dp[i][j]);
    }else{
      if(c1[scc1.group[i][0]] == c2[scc2.group[j][0]]){
	chmax(ans,dp[i][j]+1);
	for(int v : scc1.sccG[i])for(int u : scc2.sccG[j]){
	    chmax(dp[v][u], dp[i][j] + 1);
	  }
      }else{
	for(int v : scc1.sccG[i])chmax(dp[v][j], dp[i][j]);
	for(int v : scc2.sccG[j])chmax(dp[i][v], dp[i][j]);
      }
    }
  }
  cout << ans << endl;
}

Measured memory_bytes: 1532