#include<bits/stdc++.h>
#define rint register int 
#define ll long long 
using namespace std;
char S[501000],T[500100];
int N;
queue<int> q;
int main(){
	scanf("%d",&N);
	scanf("%s",S+1);
	scanf("%s",T+1);
	int las=-1;
	ll ans=0;
	for(rint i=1;i<=N;++i) {
		if(T[i]=='1') {
			q.push(i);
		}	
		if(S[i]=='1') {
			if(las!=-1) {
				ans+=i-las;
				las=-1;
			}
			else {
				if(!q.empty()) {ans+=i-q.front();q.pop();}
				else las=i;
			}
		}
	}
	if((!q.empty())||(las!=-1)) {
		cout<<-1<<"\n";
	}
	else cout<<ans<<"\n";
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

#if DEBUG && !ONLINE_JUDGE
    ifstream input_from_file("input.txt");
    #define cin input_from_file
#else

#endif

int N, L;
vector<LL> A, B;



LL goingleft(int l, int r) {
    if (B[l] != A[l]) return -1;
    if (l == r) return 0;
    if (A[r] == B[r]) return goingleft(l, r-1);
    int n = r;
    while (n > l and A[n] + r-n > B[r]) n--;
    if (A[n] + r-n != B[r]) return -1;
    int m = r;
    while (m > l and A[n] + m-n == B[m]) m--;
    LL newres = goingleft(l, m);
    if (newres == -1) return -1;
    return newres + r-n;
}


LL goingright(int l, int r) {
    if (B[r] != A[r]) return -1;
    if (l == r) return 0;
    if (A[l] == B[l]) return goingright(l+1, r);
    int n = l;
    while (n < r and A[n] + l-n < B[l]) n++;
    if (A[n] + l-n != B[l]) return -1;
    int m = l;
    while (m < r and A[n] + m-n == B[m]) m++;
    LL newres = goingright(m, r);
    if (newres == -1) return -1;
    return newres + n-l;
}

LL result(int n) {
    //~ cout << n << endl;
    if (n == 0) return 0;
    if (B[n] == A[n]) return result(n-1);
    if (B[n] > A[n]) {
        int m = n;
        while (B[m] > A[m]) m--;
        LL res1 = result(m);
        LL res2 = goingright(m+1, n+1);
        //~ cout << "going right " << m+1 << " " << n <<  " " << res1 << " " << res2 << endl;
        if (res1 == -1 or res2 == -1) return -1;
        return res1 + res2;
    }
    int m = n;
    while (B[m] < A[m]) m--;
    LL res1 = result(m);
    LL res2 = goingleft(m, n);
    if (res1 == -1 or res2 == -1) return -1;
    //~ cout << "going left " << m+1 << " " << n << " " << res1 << " " << res2 << endl;
    return res1 + res2;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); // Togliere nei problemi con query online

    cin >> N >> L;
    A.resize(N+2), B.resize(N+2);
    A[0] = 0, B[0] = 0;
    A[N+1] = L+1, B[N+1] = L+1;
    for (int i = 1; i <= N; i++) cin >> A[i];
    for (int i = 1; i <= N; i++) cin >> B[i];
    cout << result(N) << "\n";
}
