#include<bits/stdc++.h>
using namespace std;
#define int long long
#define X first
#define Y second

int n;
vector<pair<int,int>> shart(100);
vector<pair<int,int>> aadmi(16);
int res=0;

void fun(int pers, int m, int k, vector<int> coin)
{
    if(pers==k)
    {
        bool bartan[101]={};
        for(auto x: coin)  bartan[x]=true;
        int cnt=0;
        for(int i=0;i<m;i++) if(bartan[shart[i].X] && bartan[shart[i].Y]) cnt++;
        res=max(res,cnt); 
    }
    else
    {
        coin.push_back(aadmi[pers].X); 
        fun(pers+1,m,k,coin);
        coin.pop_back();
        coin.push_back(aadmi[pers].Y); 
        fun(pers+1,m,k,coin);
        coin.pop_back();
    }
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int tc=1;
    // cin>>tc;
    while(tc--)
    {
        int m,k;
        cin>>n>>m;
        for(int i=0;i<m;i++) cin>>shart[i].X>>shart[i].Y;
        cin>>k;
        for(int i=0;i<k;i++) cin>>aadmi[i].X>>aadmi[i].Y;
        vector<int> coin;
        fun(0,m,k,coin);
        cout<<res<<"\n";
    } 
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n)    for(long long i = 0; i < (n) ; ++i)
#define orep(i,n)   for(long long i = 1; i <= (n); ++i)   // one rep
#define drep(i,n)   for(long long i = (n)-1; i >= 0; --i) // down rep
#define srep(i,s,t) for(long long i = (s); i < (t); ++i)  // set rep
#define rng(x)  (x).begin(),(x).end()

  // range
#define rrng(a) (a).rbegin(),(a).rend() // reverse range
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define ru(x,y) (((x)+(y)-1)/(y)) // round up
#define fi first
#define se second
#define eb emplace_back
#define fcout cout << fixed << setprecision(15)

using ll  = long long;
using P   = pair<int,int>;
using vi  = vector<int>;
using vvi = vector<vi>;
using vp  = vector<P>;
using vvp = vector<vp>;
using vl  = vector<ll>;
using rpq = priority_queue<int,vi,less<int>>;   // 大きい順
using pq  = priority_queue<int,vi,greater<int>>;// 小さい順

template<typename T> void Yes(T flag) {
  cout << (flag ? "Yes" : "No") << endl;
}

template<typename T> void pv(vector<T> vec) {
  cout << "[";
  for(auto& v : vec) { cout << v << ","; }
  cout << "]" << endl;
}

template<>
void pv(vector<P> vec) {
  cout << "[";
  for(auto& v : vec) {
    cout << "<" << v.fi << ","<< v.se << ">" << ",";
  }
  cout << "]" << endl;
}

template<typename T> void pv2(vector<vector<T>> vec) {
  for(auto& v : vec) pv(v);
}

int dp[1005][1005];
int main() {
  int n,m;cin >> n >> m;
  vi a(n,0),b(m,0);
  rep(i,n) cin >> a.at(i);
  rep(i,m) cin >> b.at(i);

  rep(i,n+1) rep(j,m+1) {
    dp[i][j] = 1001001001;
  }
  dp[0][0] = 0;

  rep(i,n+1) rep(j,m+1) {
    if(i>0 and j > 0) {
      if(a.at(i-1) == b.at(j-1)) {
        dp[i][j] = min(dp[i][j],dp[i-1][j-1]);
      } else {
        dp[i][j] = min(dp[i][j],dp[i-1][j-1] + 1);
      }
    }
      if(i>0) dp[i][j] = min(dp[i][j],dp[i-1][j] + 1);
      if(j>0) dp[i][j] = min(dp[i][j],dp[i][j-1]+1);
  }
  cout << dp[n][m] << endl;

  return 0;
}
