#include<bits/stdc++.h>
const int MAXN = 4e5 + 10;
using namespace std;
int n;
int A[MAXN], B[MAXN], ta[MAXN], tb[MAXN], sa, sb, tot = 0, date[MAXN], fa[MAXN];
int find(int x) {
    return fa[x] == x ? fa[x] : fa[x] = find(fa[x]);
}
int unionn(int x, int y) {
    fa[x] = y;
}
int main() {
    scanf("%d",&n);
    for(int i = 1; i <= n; i++)scanf("%d",&A[i]), sa ^= A[i]; A[n + 1] = sa;
    for(int i = 1; i <= n; i++)scanf("%d",&B[i]);sb ^= B[i]; B[n + 1] = sb;
    n++;
    memcpy(ta, A, sizeof(A)); memcpy(tb, B, sizeof(B));
    sort(ta + 1, ta + n + 1); sort(tb + 1, tb + n + 1);
    for(int i = 1; i <= n - 1; i++) if(ta[i] != tb[i]) return puts("-1"), 0;
 
    int ans = 0, sum = 0;
    for(int i = 1; i <= n; i++) 
        if(A[i] != B[i] || (i == n)) {
            date[++sum] = A[i]; date[++sum] = B[i];
            if(i < n)ans++;
        }
    if(ans == 0) return puts("0"), 0;
 
    sort(date + 1, date + sum + 1);
    sum = unique(date + 1, date + sum + 1) - date - 1;
    for(int i = 1; i <= sum; i++) fa[i] = i;
    for(int i = 1; i <= n; i++)
        if(A[i] != B[i]) {
            A[i] = lower_bound(date + 1, date + sum + 1, A[i]) - date;
            B[i] = lower_bound(date + 1, date + sum + 1, B[i]) - date;
            unionn(find(A[i]), find(B[i]));
        }
 
    for(int i = 1; i <= sum; i++)
        if(fa[i] == i) ans++;
    printf("%d", ans - 1);
 
    return 0;
}