#include <iostream>
using namespace std;
#include <vector>
#include <set>
#include<string>
#include <algorithm>
#include <cmath>
#include <climits>
typedef long long ll;
typedef unsigned long long ull;


//AtCoder Begginer Contest 201 C
int main(void) 
{
	int n;
	cin >> n;

	vector<ll> a(n);

	for (auto& x : a)
	{
		cin >> x;
	}

	ll min = LONG_MAX -1 ;
	
	ll max_i = (1 << (n - 1) ) - 1; //2の(n-1)乗-1

	for (ll i = 0; i <= max_i; i++)
	{
		vector<bool> s(n - 1);
		int count_s = 0; //仕切りの数

		for (int j = 0; j < n - 1; j++)
		{
			s[j] = (i >> j) & 1;
			count_s += s[j];
		}

		ll xorall = 0;
		ll or_tmp = 0;
		for (int j = 0; j < n; j++)
		{
			or_tmp |= a[j];

			bool bRun_xor = false;
			if (j < n - 1)
			{
				if (true == s[j])
				{
					bRun_xor = true;
				}
			}
			else
			{
				bRun_xor = true;
			}

			if (true == bRun_xor)
			{
				xorall ^= or_tmp;
				or_tmp = 0;
			}
			
		}

		min = std::min(xorall, min);

	}

	cout << min;

	return 0;
}
#include <bits/stdc++.h>
typedef long long LL;
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RALL(X) (X).rbegin(), (X).rend()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define DRVI(N, X) VI X(N); for (int ___I=0; ___I<N; ___I++) scanf("%d", &(X[___I]))  
#define RS(X) scanf("%s", (X))
#define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define PII pair<int,int>
#define VI vector<int>
#define VL vector<long long>
#define VPII vector<pair<int,int> >
#define PLL pair<long long,long long>
#define VPLL vector<pair<long long,long long> >
#define F first
#define S second

using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;
    string s;
    string t;
    cin>>s>>t;
    VI a;
    VI b;
    REP(i,n){
        if (s[i]=='1') a.PB(i);
        if (t[i]=='1') b.PB(i);
    }
    int st=SZ(b);
    int ss=SZ(a);
    bool isok=true;
    int i=0;
    int j=0;
    LL ops=0;
    if (st>ss or st%2!=ss%2) isok=false;
    //cout<<isok<<endl;
    while (i<ss and j<st){
        //cout<<i<<" "<<j<<" "<<a[i]<<" "<<b[j]<<endl;
        if (b[j]>a[i]){
            if (i==ss-1) {isok=false; break;}
            else ops+=a[i+1]-a[i];
            i+=2;
        }
        else{
            ops+=a[i]-b[j];
            i++; j++;
        }
    }
    //cout<<isok<<endl;
    if (j<st) {isok=false;}
    while (i<ss){
        if (i==ss-1) {isok=false; break;}
        else ops+=a[i+1]-a[i];
        i+=2;
    }

    if (isok) cout<<ops<<"\n";
    else cout<<"-1\n";
}
