#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M), B(M);
  rep(i, M) { cin >> A[i] >> B[i]; }
  int K;
  cin >> K;
  vector<int> C(K), D(K);
  rep(i, K) { cin >> C[i] >> D[i]; }
  int res = 0;
  for (int mask = 0; mask < (1 << K); mask++) {
    vector<bool> dishes(N + 1);
    rep(i, K) {
      if (mask & (1 << i)) {
        dishes[C[i]] = true;
      } else {
        dishes[D[i]] = true;
      }
    }
    int cnt = 0;
    rep(i, M) {
      if (dishes[A[i]] && dishes[B[i]]) {
        cnt++;
      }
    }
    res = max(res, cnt);
  }
  cout << res << endl;
  return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define endl '\n'
#define all(x) (x).begin(),(x).end()
const int INF=1000000000+5;
const int N=1e5+5;
const ll oo=1e18+5;
const ll mod=1e9+7;

map<int,pair<int,int>> get(int n,int L){
	vector<int> v{0};
	for(int i=1;i<=n+1;++i){
		int x=L+1;
		if(i<=n) cin>>x;
		v.push_back(x-i);
	}
	map<int,pair<int,int>> interval;
	for(int i=0;i<(int)v.size();++i){
		if(!i||v[i]!=v[i-1]){
			interval[v[i]].first=i;
		}
		interval[v[i]].second=i;
	}
	return interval;
}

int main(){
	ios::sync_with_stdio(0); cin.tie(0);
	int n,L;
	cin>>n>>L;
	auto a=get(n,L);
	auto b=get(n,L);
	//~ cout<<"a : "<<endl;
	//~ for(auto x:a){
		//~ cout<<"("<<x.first<<")["<<x.second.first<<","<<x.second.second<<"]"<<endl;
	//~ }
	//~ cout<<"b : "<<endl;
	//~ for(auto x:b){
		//~ cout<<"("<<x.first<<")["<<x.second.first<<","<<x.second.second<<"]"<<endl;
	//~ }
	ll ans=0;
	for(auto x:b){
		int val=x.first;
		if(!a.count(val)){
			return cout<<-1,0;
		}
		ans+=max(0,a[val].first	-b[val].first)+max(0,b[val].second-a[val].second);
	}
	cout<<ans;
	return 0;
}
