#include <iostream>
using namespace std;

void no() { cout << "-1" << endl; }

int main() {
    int n; cin >> n;
    string s, t; cin >> s >> t;
    long long ans = 0;
    int i = 0, j = 0;
    while (j < n) {
        if (t[j] == '1') {
            while (i < n && s[i] == '0') i++;
            if (i == n) { no(); return 0; }
            if (i < j) {
                int i2 = i + 1;
                while (i2 < n && s[i2] == '0') i2++;
                if (i2 == n) { no(); return 0; }
                ans += i2 - i;
                i = i2 + 1;
            } else {
                ans += i - j;
                i++; j++;
            }
        } else {
            j++;
        }
        // cerr << i << ' ' << j << ' ' << ans << endl;
    }
    while (i < n) {
        if (s[i] == '0')  { i++; }
        else {
            int k = i + 1;
            while (k < n && s[k] == '0') k++;
            if (k == n) { no(); return 0; }
            ans += k - i;
            i = k + 1;
        }
        // cerr << i << ' ' << j << ' ' << ans << endl;
    }
    cout << ans << endl;
}

#include<bits/stdc++.h>
using namespace std;
using LL=long long;
using P=pair<int,int>;
using T=tuple<int,int,int>;
constexpr int mod=1000000007;
constexpr int inf=1e9;
constexpr double eps=1e-13;
int in(){
    int x;
    scanf("%d",&x);
    return x;
}
int main(){
    int a=in(),b=in(),c=in();
    int ans[10][4]={{0,0,0,0},{1,1,1,1},{6,2,4,8},{1,3,9,7},{6,4,6,4},{5,5,5,5},{6,6,6,6},{1,7,9,3},{6,8,4,2},{1,9,1,9}};
    int mod4;
    if(b%4==0)mod4=0;
    if(b%4==1)mod4=1;
    if(b%4==2)mod4=(c==1 ? 2 : 0);
    if(b%4==3)mod4=(c%2 ? 3 : 1);
    cout<<ans[a%10][mod4]<<endl;
    return 0;
}