#define ENABLE_DEBUG 1
// Kana's kitchen {{{
#include<bits/stdc++.h>
#define ALL(v) std::begin(v), std::end(v)
#define REP(i, k) for (decltype(k) i = 0; i < k; i++)
#define LOOP(k) REP (ngtkana_is_a_genius, k)

using i32 = std::int_least32_t;
using i64 = std::int_least64_t;
using u32 = std::uint_least32_t;
using u64 = std::uint_least64_t;
using usize = std::size_t;

template <class T, class U> using pair = std::pair<U, T>;
template <class T> using diag_pair = std::pair<T, T>;
template <class... Args> using tuple = std::tuple<Args...>;
template <class T> using vec = std::vector<T>;
template <class T> using numr = std::numeric_limits<T>;

#ifdef NGTKANA
#include<debug.hpp>
#else
#define DEBUG(...)(void)0
#endif
/*}}}*/
auto xor_fn = [](auto x, auto y) { return x ^ y; };
void err() {
    std::cout << -1 << '\n';
    exit(0);
}
int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    std::cout << std::setprecision(15) << std::fixed;

    u32 n; std::cin >> n;
    auto compress = [n](auto a) {
        vec<u32> m = a;
        std::sort(ALL(m));
        m.resize(std::unique(ALL(m)) - m.begin());
        for (u32& x: a) x = std::lower_bound(ALL(m), x) - m.begin();
        return a;
    };
    vec<u32> a(n), b(n);
    for (u32& x: a) std::cin >> x;
    for (u32& x: b) std::cin >> x;
    a.push_back(std::accumulate(ALL(a), u32{0}, xor_fn));
    b.push_back(std::accumulate(ALL(b), u32{0}, xor_fn));
    {
        auto a_ = a;
        auto b_ = b;
        std::sort(ALL(a_));
        std::sort(ALL(b_));
        if (a_!=b_) err();
    }

    auto a_cmp = compress(a);
    auto b_cmp = compress(b);
    u32 sz = 1 + *std::max_element(ALL(a_cmp));
    vec<vec<u32>> g(sz);
    REP (i, n+1) if (a_cmp.at(i)!=b_cmp.at(i)) {
        g.at(a_cmp.at(i)).push_back(b_cmp.at(i));
        g.at(b_cmp.at(i)).push_back(a_cmp.at(i));
    }

    vec<u32> ckd(sz);
    auto dfs = [&](auto&& f, u32 x) -> void {
        ckd.at(x) = true;
        for (u32 y: g.at(x)) if (!ckd.at(y)) f(f, y);
    };

    u32 ans = 0;
    REP (i, sz) ans += g.at(i).size();
    ans /= 2;
    for (u32 i=0; i<sz; i++) if (!ckd.at(i)) {
        dfs(dfs, i);
        if (!g.at(i).empty()) ans++;
    }
    u32 p = std::lower_bound(ALL(a_cmp), a.at(n)) - a_cmp.begin();
    if (!g.at(p).empty()) {
        if (a.at(n)!=b.at(n)) ans-=2;
        else ans--;
    }
    std::cout << ans << '\n';
}
