#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int N;
int a[MAXN], b[MAXN];
map<int, queue<int> > pos;
int perm[MAXN];
bool bio[MAXN];
void load() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) scanf("%d", a + i);
  for (int i = 0; i < N; i++) scanf("%d", b + i);
}
void dfs(int x) {
  if (bio[x]) return;
  bio[x] = true;
  dfs(perm[x]);
}
int solve() {
  for (int i = 0; i < N; i++) a[N] ^= a[i];
  for (int i = 0; i < N; i++) pos[b[i]].push(i);
  int cnt = 0;
  for (int i = 0; i <= N; i++)
    if (pos[a[i]].empty()) {
      perm[i] = N;
      cnt++;
    } else {
      perm[i] = pos[a[i]].front();
      pos[a[i]].pop();
    }
  if (cnt > 1) return -1;
  dfs(N);
  int sol = 0;
  for (int i = 0; i < N; i++)
    if (perm[i] != i) {
      sol++;
      if (!bio[i]) {
        dfs(i);
        sol++;
      }
    }
  if (perm[N] == N)
    for (int i = 0; i < N; i++)
      if (a[i] == a[N]) {
        sol--;
        break;
      }
  return sol;
}
int main() {
  load();
  printf("%d\n", solve());
  return 0;
}
