#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
char In[1 << 20], *ss = In, *tt = In;
#define getchar() (ss == tt && (tt = (ss = In) + fread(In, 1, 1 << 20, stdin), ss == tt) ? EOF : *ss++)
ll read() {
	ll x = 0, f = 1; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + int(ch - '0');
	return x * f;
}
int read01() {
	char ch = getchar();
	for(; ch != '0' && ch != '1'; ch = getchar());
	return ch - '0';
}
const int MAXN = 5e5 + 5;
int n, a[MAXN], b[MAXN], qa[MAXN], hda, tla, qb[MAXN], hdb, tlb, to[MAXN];
ll ans;
vector<int> le, ri;
int main() {
	n = read();
	for(int i = 1; i <= n; i++) a[i] = read01();
	for(int i = 1; i <= n; i++) b[i] = read01();
	hda = hdb = 1; tla = tlb = 0;
	for(int i = 1; i <= n; i++) {
		if(a[i]) {
			if(hdb <= tlb) {
				to[i] = qb[hdb++]; le.push_back(i);
			} else qa[++tla] = i;
		}
		if(b[i]) {
			if(hda <= tla) {
				to[qa[hda]] = i; ri.push_back(qa[hda]);
				hda++;
			} else qb[++tlb] = i;
		}
	}
	if(hdb <= tlb || hda <= tla) {
		printf("-1\n");
		return 0;
	}
	if(le.size()) ans += le[0] - to[le[0]];
	for(int i = 1; i < (int)le.size(); i++) {
		ans += le[i] - max(to[le[i]], le[i-1] + 1);
	}
	if(ri.size()) ans += to[ri[ri.size()-1]] - ri[ri.size()-1];
	for(int i = (int)ri.size() - 2; i >= 0; i--) {
		ans += min(to[ri[i]], ri[i+1] - 1) - ri[i];
	}
	printf("%lld\n", ans);
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    int ans = INT_MAX;
    for (int i = 0; i < (1<<(n-1)); i++)
    {
        int in, out;
        in = out = 0;
        for (int j = 0; j < n; j++)
        {
            in |= a[j];
            if (i&1<<j)
            {
                out ^= in;
                in = 0;
            }
        }
        out ^= in;
        ans = min(ans, out);
    }
    cout << ans << endl;
}
