#include <bits/stdc++.h>
#define SZ(x) ((int) (x).size())
using namespace std;

void noSol() {
    cout << "-1\n";
    exit(0);
}

template<class T>
vector<T> sorted(const vector<T>& a) {
    vector<T> b = a;
    sort(b.begin(), b.end());
    return b;
}

vector<int> cc;
vector<vector<int>> graph;

int dfs(int node, int ccc) {
    cc[node] = ccc;
    int ret = SZ(graph[node]);
    for (int to: graph[node]) {
        if (cc[to] == -1) {
            ret += dfs(to, ccc);
        }
    }
    return ret;
}

int solve(vector<int> a, vector<int> b) {
    vector<int> all = a;
    all.insert(all.end(), b.begin(), b.end());
    sort(all.begin(), all.end());
    all.erase(unique(all.begin(), all.end()), all.end());
    for (int& x: a) {
        x = lower_bound(all.begin(), all.end(), x) - all.begin();
    }
    for (int& x: b) {
        x = lower_bound(all.begin(), all.end(), x) - all.begin();
    }
    int n = SZ(a);
    cc = vector<int>(n, -1);
    graph = vector<vector<int>>(n);
    for (int i = 0; i < n; ++i) {
        if (a[i] != b[i]) {
            graph[a[i]].push_back(b[i]);
            graph[b[i]].push_back(a[i]);
        }
    }
    int ccc = 0;
    int ans = 0;
    for (int i = 0; i < n; ++i) {
        if (cc[i] == -1 && !graph[i].empty()) {
            ans += dfs(i, ccc++);
        }
    }
    ans /= 2;
    ans += ccc;
    return ans;
}

int main() {
    #ifdef LOCAL_RUN
    freopen("task.in", "r", stdin);
    freopen("task.out", "w", stdout);
    //freopen("task.err", "w", stderr);
    #endif // ONLINE_JUDGE
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < n; ++i) {
        cin >> b[i];
    }
    const int INF = 0x3f3f3f3f;
    int ans = INF;
    if (sorted(a) == sorted(b)) {
        ans = solve(a, b);
        int x = 0;
        for (int val: a) x ^= val;
        for (int i = 0; i < n; ++i) {
            if (a[i] != b[i] && a[i] == x) {
                ans--;
                break;
            }
        }
        cout << ans << '\n';
    } else {
        map<int, int> ma, mb;
        int x = 0;
        for (int val: a) x ^= val;
        for (int val: a) ma[val]++;
        for (int val: b) mb[val]++;
        int add = 0;
        for (const pair<int, int>& p: ma) {
            add += max(0, p.second - mb[p.first]);
        }
        if (add > 1) {
            noSol();
        }
        int bad = -1;
        for (const pair<int, int>& p: ma) {
            if (p.second > mb[p.first]) {
                bad = p.first;
                break;
            }
        }
        if (bad == x) {
            noSol();
        }
        int pos = -1;
        for (int i = 0; i < n; ++i) {
            if (a[i] == bad && a[i] != b[i] && b[i] != x) {
                pos = i;
                swap(x, a[i]);
                break;
            }
        }
        if (pos != -1) {
            int ans = solve(a, b) - 1;
            // for (int i = 0; i < n; ++i) {
            //     if (a[i] != b[i] && a[i] == x && cc[i] != cc[pos]) {
            //         ans--;
            //         break;
            //     }
            // }
            cout << ans << '\n';
        } else {
            for (int i = 0; i < n; ++i) {
                if (a[i] == bad && a[i] != b[i]) {
                    pos = i;
                    swap(x, a[i]);
                    break;
                }
            }
            int ans = solve(a, b) + 1;
            for (int i = 0; i < n; ++i) {
                if (a[i] == a[pos] && a[i] != b[i]) {
                    ans--;
                    break;
                }
            }
            // for (int i = 0; i < n; ++i) {
            //     if (a[i] != b[i] && a[i] == x && cc[i] != cc[pos]) {
            //         ans--;
            //         break;
            //     }
            // }
            cout << ans << '\n';
        }
    }
}


Measured memory_bytes: 19824