#include <bits/stdc++.h>
using namespace std;
#define clr(a) memset(a, 0, sizeof(a))
#define full(a) memset(a, 0x3f, sizeof(a))
#define fornext(x, i) for(int i = head[x], y = ver[i]; i; i = nxt[i], y = ver[i])
#define mset(a, b) memset(a, b, sizeof(a))
#define Rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define dRep(i, a, b) for(int i = (a); i >= (b); --i)
#define IOset(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout);
#define endl '\n'
#define int ll
#define un unsigned
#define ll long long
#define db double
#define rept cerr<<1.0*clock()/CLOCKS_PER_SEC<<endl;
#define dbg cout<<"c "
#define dbug(x) cerr << #x " = " << (x) << endl
template<typename _T> inline void _prt(_T _d) { cerr << _d << ' '; }
template <typename _T, typename ...Args> inline void _prt(_T _d, Args ..._a) { _prt(_d), _prt(_a...); }
template <typename _T, typename ...Args> inline void prt(_T _d, Args ..._a) { _prt(_d), _prt(_a...), cerr<<endl; }
template <typename _T, typename ...Args> inline void prt(_T _d) { cerr << _d <<endl; }
template <typename _T, typename ...Args> inline void cbg(_T _d, Args ..._a) { cerr << "--LINE " << __LINE__ << "--: "; prt(_d, _a...); }
template<typename _T> inline void rd(_T &_d) {
	signed _f; char _c; _f = _d = 0; while (_c = getchar(), !isdigit(_c)) _f = _f || (_c == '-');
	while (isdigit(_c)) _d = _d * 10 + _c - '0', _c = getchar();
	_d = (_f ? -_d : _d);
}
template <typename _T, typename ...Args> inline void rd(_T &_d, Args &..._a) { rd(_d); rd(_a...); }

const int N=5e5+5;
int n;
char s[N],t[N];
stack<int> st;

void no() {
	puts("-1"); exit(0);
}

signed main() {
	rd(n);
	scanf("%s",s+1);
	scanf("%s",t+1);
	Rep(i,1,n) s[i]-='0';
	Rep(i,1,n) t[i]-='0';
	int ans=0;
	dRep(i,n,1) {
		if(s[i]==t[i]) continue;
		if(s[i]==1) st.push(i);
		else {
			if(st.empty()) no();
			else ans+=st.top()-i,st.pop();
		}
	}
	int x;
	if(st.size()&1) no();
	while(st.size()) x=st.top(),st.pop(),ans+=st.top()-x,st.pop();
	cout<<ans<<endl;
	return 0;
}
#include<bits/stdc++.h>
using namespace std;

const int N=1e5+1e3+7;

int n,f[N],sz[N];

vector<int>g[N];

int main()
{
    scanf("%d",&n);
    for(int i=2;i<=n;i++)
    {
        int p;
        scanf("%d",&p);
        g[p].push_back(i);
    }
    for(int i=n;i>=1;i--)
    {
        int x=i;
        f[x]=1;
        sz[x]=1;
        vector<int>cho;
        int sum=0;
        for(auto v:g[x])
        {
            sz[x]+=sz[v];
            if(sz[v]&1)
                cho.push_back(f[v]);
            else if(f[v]<0)
                f[x]+=f[v];
            else
                sum+=f[v];
        }
        sort(cho.begin(),cho.end());
        cho.push_back(sum);
        for(int i=0;i<cho.size();i++)
            if(i&1)
                f[x]-=cho[i];
            else
                f[x]+=cho[i];
    }
    printf("%d\n",(f[1]+n)/2);
}