#include <bits/stdc++.h>
#define owo(i,a, b) for(int i=(a);i<(b); ++i)
#define uwu(i,a, b) for(int i=(a)-1; i>=(b); --i)
#define senpai push_back
#define ttgl pair<int, int>
#define ayaya cout<<"ayaya~"<<endl
 
using namespace std;
using ll = long long;
using ld = long double;
ll MOD = 1000000007;
const ll root = 3;
ll binpow(ll a,ll b){ll res=1;while(b){if(b&1)res=(res*a)%MOD;a=(a*a)%MOD;b>>=1;}return res;}
ll modInv(ll a){return binpow(a, MOD-2);}
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll NINFLL = 0xc0c0c0c0c0c0c0c0;
const int mxN = 200001;
vector<int> adj[mxN];
int dp[mxN];
int sz[mxN];
bool done[mxN];
int n;
void dfs(int u) {
    sz[u] = 1;
    vector<ttgl> t, o;
    vector<int> rst;
    int numod = 0;
    for(int v: adj[u]) {
        dfs(v);
        if(sz[v]&1){
            t.senpai({2*dp[v] - sz[v], v});
            numod++;
        }else {
            rst.senpai(v);
        }
        sz[u]+=sz[v];
    }
    sort(t.begin(), t.end());
    owo(_, 0, numod) {
        if(_&1) {
            //while(done[t[tid].second])tid--;
            dp[u] += sz[t[_].second] - dp[t[_].second];
        }else {//aoki choose a dp[0]
            //while(done[t[tid].second])tid++;
            dp[u]+=dp[t[_].second];
        }
    }
    for(int k: rst) {
        if(dp[k] < sz[k] - dp[k]) {
            dp[u]+=dp[k];
        }else{
            if(numod%2==0) dp[u]+=dp[k];
            else dp[u]+=sz[k]-dp[k];
        }
    }
    dp[u]++;
}
int main() {
    //freopen("file.in", "r", stdin);
    //freopen("file.out", "w", stdout);
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    cin.tie(0)->sync_with_stdio(0);
    cin>>n;
    int a;
    owo(i, 1, n) {
        cin>>a;
        a--;
        adj[a].senpai(i);
    }
    dfs(0);
    cout<<dp[0]<<"\n";
    return 0;
}
#include <bits/stdc++.h>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
#define ll long long
//#define file
using namespace std;

int n,i,j,k,l;
char S[500001];
char T[500001];
int s1[500001],s2[500001];
int ans,sum;

int main()
{
	#ifdef file
	freopen("b.in","r",stdin);
	#endif
	
	scanf("%d",&n);
	scanf("%s",S+1);
	scanf("%s",T+1);
	
	fo(i,1,n)
	{
		s1[i]=s1[i-1]+(S[i]=='0');
		s2[i]=s2[i-1]+(T[i]=='0');
	}
	fo(i,1,n) sum+=S[i]=='0';
	ans=sum;
	fo(i,1,n) sum-=T[i]=='0';
	if (sum) {printf("-1\n");return 0;}
	
	fo(i,1,n)
	if (S[i]==T[i] && S[i]=='0' && s1[i]==s2[i])
	--ans;
	
	printf("%d\n",ans);
	
	fclose(stdin);
	fclose(stdout);
	return 0;
}