#include <iostream>
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long
#define max_value 0x3f3f3f
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
typedef pair<ll,ll> pii;

const int  inf=1e9+7;
const int mod=998244353;
const int maxn=1e3+1;
ll sqr(ll x){
return x*x;
}
ll modexpo(ll a,ll b){
    a%=inf;
ll res=1;
 while(b>0){
    if(b&1)
        res=res*a%inf;
    a=a*a%inf;
    b>>=1;
 }
 return res;

}


void solve(){
int n;
cin>>n;
string s;
cin>>s;
string t;
cin>>t;
vector<int>v,v1;
for(int i=0;i<n;i++){
    if(s[i]=='0')
        v.push_back(i);
    if(t[i]=='0')
        v1.push_back(i);
}
if((int)v.size()!=(int)v1.size())
cout<<"-1";
else{
        int cnt=0;
    for(int i=0;i<v1.size();i++){
        if(v1[i]!=v[i]){
           cnt++;
        }
    }
   cout<<cnt;
}

}





























int main(){
    FIO;
  /*file;*/
 int t;
t=1;
 while(t--){
 solve();
}

}








#include <bits/stdc++.h>

using namespace std;
using ll =long long;
typedef pair<ll,ll> P;
#define SORT(a) sort((a).begin(),(a).end())
#define REV(a) reverse((a).begin(),(a).end())
#define For(i, a, b)    for(int i = (a) ; i < (b) ; ++i)
#define rep(i, n)       For(i, 0, n)
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

void coY() {cout <<"Yes"<<endl;}
void coN(){cout <<"No"<<endl;}
void mswap(ll &a, ll &b){ if(a >= b) swap(a,b); }
void rswap(ll &a, ll &b){ if(a <= b) swap(a,b); }

const int dy[] = {0,0,1,-1};
const int dx[] = {1,-1,0,0};
const ll mod = 1e9+7;
const ll MOD = 998244353;
const double PI=3.14159265358979323846;
const int inf = 1001001001;
const ll INF = 1e18;
//Write From this Line
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 ans = 0;
	for(int bit = 0; bit < (1 << k); bit++){
		bitset<16> tmp(bit);
		int now = 0;
		map<int, bool> mp;
		rep(i,k){
			// bit が立っていたらc[i]を使う
			if(tmp.test(i)){
				mp[c[i]] = true;
			} else {
				mp[d[i]] = true;
			}
		}
		// 何個の条件を満たすことが出来ているか
		rep(i,m){
			if(mp[a[i]] && mp[b[i]]){
				now++;
			}
		}
		chmax(ans,now);
	}
	cout << ans << endl;
}
