#include <bits/stdc++.h>
using namespace std;

#define debug(x) cerr << #x << '=' << x << endl
#define rep(i, b, e) for (int i = b; i <= e; i++)

char s1[512345], s2[512345];
vector<int> a(512345);
vector<int> b(512345);
signed main(void) {
    int n, cnt = 0;
    cin >> n;
    scanf("%s%s", s1 + 1, s2 + 1);
    rep (i, 1, n) {
        if (s1[i] == '0') {
            cnt++;
        }
        if (s2[i] == '0') {
            cnt--;
        }
    }
    if (cnt != 0) {
        puts("-1");
        return 0;
    }
    int ans = 0;
    rep (i, 1, n) {
        if (s1[i] == '0') {
            a.push_back(i);
        }
        if (s2[i] == '0') {
            b.push_back(i);
        }
    }
    rep (i, 0, min(a.size(), b.size()) - 1) {
        if (a[i] != b[i]) {
            ans++;
        }
    }
    cout << ans;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<int> vi;
typedef vector<ll> vl;

struct UnionFind {
    vector<int> par;
    map<int, int> groups;

    UnionFind(int n) : par(n) {
        rep(i, n) par[i] = i;
        rep(i, n) groups[i] = 1;
    }

    int root(int x) {
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return;
        // if (groups[rx] < groups[ry]) swap(rx, ry);
        groups[rx] += groups[ry];
        groups.erase(ry);
        par[ry] = rx;
    }

    bool same(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};

// input
int N, Q;
vi C;

map<int, map<int, int>> m;


void input() {
    cin >> N >> Q;
    C = vi(N);
    rep(i, N) cin >> C[i];
}


void query1(UnionFind &uf) {
    int a, b;
    cin >> a >> b;
    int r1 = uf.root(a - 1), r2 = uf.root(b - 1);
    if (r1 == r2) return;
    if (m[r1].size() < m[r2].size()) swap(r1, r2);
    uf.unite(r1, r2);
    for (auto p : m[r2]) {
        m[r1][p.first] += p.second;
    }
}


void query2(UnionFind &uf) {
    int x, y;
    cin >> x >> y;
    int r = uf.root(x - 1);
    int ans = m[r][y];
    cout << ans << endl;
}


int main() {
    input();
    UnionFind uf(N);
    rep(i, N) m[i][C[i]] = 1;
    rep(i, Q) {
        int x;
        cin >> x;
        if (x == 1) query1(uf);
        else query2(uf);
    }
}
