#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <regex>

/* g++ -g -std=c++0x */
/* g++ -g -std=c++11 */

using namespace std;

// std::ios::sync_with_stdio(false);

// freopen("input.txt", "rt", stdin);
// freopen("output.txt", "wt", stdout);

#define ALL(c)          (c).begin(), (c).end()
#define ALLR(c)         (c).rbegin(), (c).rend()
#define FOR(i,a,b)      for (int i=(a); i < (b); ++i)
#define FORR(i,a,b)     for (int i=(a); i > (b); --i)
#define FOR_ALL(i,c)    for (__typeof((c).begin()) i=(c).begin();   \
                             i != (c).end(); ++i)
#define FOR_ALLR(i,c)   for (__typeof((c).rbegin()) i=(c).rbegin(); \
                             i != (c).rend(); ++i)
#define SZ(array)       (sizeof(array)/sizeof(array[0]))
#define lc(x)           (x<<1)     /* 2*x */
#define rc(x)           (x<<1 | 1) /* 2*x+1 */
#define lowbit(x)       (x & (-x)) /* 0b10100 -> 0b100 */

typedef long long       LL;
typedef map<int,int>    MII;
typedef pair<int,int>   PII;
typedef set<int>        SI;
typedef vector<bool>    VB;
typedef vector<double>  VD;
typedef vector<int>     VI;
typedef vector<string>  VS;

/* check if a key is in container C */
template <class C>
inline bool in_(const typename C::key_type& k, const C& A)
{ return A.find(k) != A.end(); }
inline bool in_(const string& s, const string& S)
{ return S.find(s) != string::npos; }

const int maxn = 1e5+5;
int A[maxn],B[maxn],C[maxn],D[maxn],vis[maxn];
stack<int> pos[maxn];

void dfs(const int i)
{
    vis[i]++;
    const int b = B[i];
    while (pos[b].size()) {
        const int j = pos[b].top(); pos[b].pop();
        if (!vis[j])
            dfs(j);
    }
}

int main()
{
/* #ifndef ONLINE_JUDGE */
/*     freopen("foo", "rt", stdin); */
/* #endif */
    int N;
    while (cin >> N) {
        FOR(i,0,N+1)
            while (pos[i].size())
                pos[i].pop();
        int a0 = 0, b0 = 0;
        FOR(i,1,N+1) {
            cin >> A[i]; a0 ^= A[i]; C[i] = A[i];
        }
        A[0] = C[0] = a0;
        FOR(i,1,N+1) {
            cin >> B[i]; b0 ^= B[i]; D[i] = B[i];
        }
        B[0] = D[0] = b0;
        sort(C,C+N+1); sort(D,D+N+1);
        bool possible = true;
        FOR(i,0,N+1)
            possible &= (C[i] == D[i]);
        int ans = -1;
        if (possible) {
            ans = 0;
            FOR(i,0,N+1) {
                A[i] = lower_bound(C,C+N+1,A[i]) - C;
                B[i] = lower_bound(D,D+N+1,B[i]) - D;
                pos[A[i]].push(i);
            }
            memset(vis,0,sizeof(vis));
            FOR(i,0,N+1) {
                if (A[i] == B[i])
                    vis[i]++;
                else
                    ans += i != 0;
            }
            FOR(i,0,N+1) if (!vis[i]) {
                dfs(i);
                ans++;
            }
            if (A[0] != B[0])
                ans--;
            else {
                FOR(i,1,N+1) if (A[i] == A[0] && A[i] != B[i]) {
                    ans--; break;
                }
            }
            ans = max(0,ans);
        }
        cout << ans << endl;
    }
    return 0;
}


Measured memory_bytes: 72832