#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#include <bitset>
#include <set>
#include <cmath>
#define LL long long
#define ull unsigned long long
#define mk(t1, t2) make_pair(t1, t2)
using namespace std;
const LL mo = 1e9 + 7;
const int MAXN = 5e5 + 5;
int n;
char s[MAXN];
LL a[MAXN], b[MAXN];
LL sta[MAXN], ans, top;

void push (int &pos) {
	while (pos <= n && b[pos] == 0) ++pos;
}

int main() {
#ifdef IN
	freopen ("x.in", "r", stdin);
	freopen ("x.out", "w", stdout); 
#endif
	int i,j,k;
	scanf ("%d", &n);
	scanf ("%s", s + 1);
	for (i=1; i<=n; ++i) a[i] = s[i] - '0';
	scanf ("%s", s + 1);
	for (i=1; i<=n; ++i) b[i] = s[i] - '0';
	j = 1, push (j);
	for (i=1; i<=n; ++i) {
		if (a[i] == 1) {
			if (top) ans += i - sta[top--];
			else if (j <= i && j <= n) ans += i - j, ++j, push (j);
			else sta[++top] = i;
		}
	}
	if (j <= n || top) printf ("-1");
	else printf ("%lld", ans);
	return 0;
}

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <istream>

#include <map>
#include <math.h>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

namespace asl
{
    typedef long long i64;
    
} 
#include <stdint.h>

#include <experimental/optional>

#include <utility>

#include <tuple>

#include <random>

#define endl '\n'

using namespace std;
using namespace asl;

void solve()
{
    int n;
    cin >> n;
    string a, b;
    cin >> a >> b;

    queue<int> q;
    int last = -1;

    i64 res = 0;

    for (int i = 0; i < n; ++i)
    {
        if (b[i] == '1')
            q.push(i);

        if (a[i] == '1')
        {
            if (last != -1)
            {
                res += i - last;
                
                last = -1;
            }
            else if (!q.empty())
            {
                res += i - q.front();
                
                q.pop();
            }
            else
            {
                
                last = i;
            }
        }
    }

    if (!q.empty() || last != -1)
    {
        cout << -1 << endl;
    }
    else
    {
        cout << res << endl;
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t = 1;

    for (int i = 1; i <= t; ++i)
    {
        solve();
    }

    return 0;
}


