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

#define int long long
#define double long double
#define rep(i, n) for (int i = 0; i < (int) n; i++)
#define reps(i, n) for (int i = 1; i <= (int) n; i++)
#define all(x) (x).begin(), (x).end()
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define bit(n) (1LL << (n))
#define dump(x) cerr << #x " = " << x << endl
using vint = vector<int>;
using vvint = vector<vint>;
using pint = pair<int, int>;
constexpr double pi = 3.1415926535897932384626433832795028;
constexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b) { return a / gcd(a, b) * b; }
template<typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
template<typename T1, typename T2> inline bool chmax(T1 &a, const T2 &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template<typename T1, typename T2> inline bool chmin(T1 &a, const T2 &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
template<typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "(" << p.first << ", " << p.second << ")";
    return os;
}
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    rep(i, vec.size()) { os << vec[i] << (i + 1 == vec.size() ? "" : ", "); }
    os << "}";
    return os;
}
template<typename T> ostream &operator<<(ostream &os, const set<T> &st) {
    os << "{";
    for (auto itr = st.begin(); itr != st.end(); itr++) {
        os << *itr;
        itr++;
        if (itr != st.end()) { os << ", "; }
        itr--;
    }
    os << "}";
    return os;
}
template<typename T> ostream &operator<<(ostream &os, const multiset<T> &st) {
    os << "{";
    for (auto itr = st.begin(); itr != st.end(); itr++) {
        os << *itr;
        itr++;
        if (itr != st.end()) { os << ", "; }
        itr--;
    }
    os << "}";
    return os;
}
template<typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &mp) {
    os << "{";
    for (auto itr = mp.begin(); itr != mp.end(); itr++) {
        os << *itr;
        itr++;
        if (itr != mp.end()) { os << ", "; }
        itr--;
    }
    os << "}";
    return os;
}
template<typename T, typename U> ostream &operator<<(ostream &os, const multimap<T, U> &mp) {
    os << "{";
    for (auto itr = mp.begin(); itr != mp.end(); itr++) {
        os << *itr;
        itr++;
        if (itr != mp.end()) { os << ", "; }
        itr--;
    }
    os << "}";
    return os;
}
struct SetUp {
    SetUp() {
        cout << fixed << setprecision(20);
        cerr << fixed << setprecision(20);
    };
} setUp;

struct UnionFindTree {
    const int n;
    int cc = 0;
    std::vector<int> par, size;
    UnionFindTree(int n) : n(n), par(n), size(n) {
        for (int i = 0; i < n; i++) {
            par[i] = i;
            size[i] = 1;
        }
    }
    int getRoot(int i) {
        if (par[i] == i) { return i; }
        else { return par[i] = getRoot(par[i]); }
    }
    void unite(int i, int j) {
        if ((i = getRoot(i)) == (j = getRoot(j))) { return; }
        if (size[i] < size[j]) { std::swap(i, j); }
        par[j] = i;
        cc--;
        if (size[i] == 1) { cc++; }
        if (size[j] == 1) { cc++; }
        size[i] += size[j];
    }
    bool isSame(int i, int j) { return getRoot(i) == getRoot(j); }
    int getSize(int i) { return size[getRoot(i)]; }
};

int N;
signed main() {
    cin >> N;
    vint a(N + 1), b(N + 1);
    rep(i, N) {
        cin >> a[i];
        a[N] ^= a[i];
    }
    rep(i, N) {
        cin >> b[i];
        b[N] ^= b[i];
    }
    vint ac(a), bc(b);
    sort(all(ac)), sort(all(bc));
    rep(i, N + 1) {
        if (ac[i] != bc[i]) {
            cout << -1 << endl;
            return 0;
        }
    }
    unique(ac);
    map<int, int> zat;
    rep(i, ac.size()) { zat[ac[i]] = i; }
    UnionFindTree uft(ac.size());
    int ans = 0, op = 0;
    rep(i, N) {
        if (a[i] == b[i]) { continue; }
        ans++;
        if (b[i] == a[N]) { op = 1; }
        uft.unite(zat[a[i]], zat[b[i]]);
    }
    uft.unite(zat[a[N]], zat[b[N]]);
    ans += uft.cc - op;
    cout << ans << endl;
}

Measured memory_bytes: 11264