#include <bits/stdc++.h>
using namespace std;
inline int read() {
  bool sgn = 0;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-') c = getchar();
  if (c == '-') sgn = 1, c = getchar();
  int x = 0;
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return sgn ? -x : x;
}
inline void write(int x) {
  if (x < 0) {
    putchar('-');
    write(-x);
  } else {
    if (x >= 10) write(x / 10);
    putchar(x % 10 + '0');
  }
}
int f[100003];
void init(int n) {
  for (int i = 1; i <= n; i++) f[i] = i;
}
int find(int x) {
  if (x != f[x]) f[x] = find(f[x]);
  return f[x];
}
void Union(int x, int y) {
  int fx = find(x), fy = find(y);
  if (fx != fy) f[fx] = fy;
}
bool same(int x, int y) { return find(x) == find(y); }
int a[100003], b[100003], buc[100003];
int A[100003], B[100003];
set<int> v[100003];
map<int, int> mp;
int main() {
  int n = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read();
    a[n + 1] ^= a[i];
    if (!mp.count(a[i])) mp[a[i]] = i;
    A[i] = a[i];
  }
  if (!mp.count(a[n + 1])) mp[a[n + 1]] = n + 1;
  for (int i = 1; i <= n; i++) {
    b[i] = read();
    b[n + 1] ^= b[i];
    B[i] = b[i];
  }
  A[n + 1] = a[n + 1];
  B[n + 1] = b[n + 1];
  sort(A + 1, A + n + 2);
  sort(B + 1, B + n + 2);
  for (int i = 1; i <= n + 1; i++) {
    if (A[i] != B[i]) {
      write(-1);
      return 0;
    }
  }
  init(n + 1);
  for (int i = 1; i <= n + 1; i++) {
    if (a[i] != b[i]) Union(mp[a[i]], mp[b[i]]);
  }
  for (int i = 1; i <= n + 1; i++) {
    int fi = find(i);
    v[fi].insert(i);
    if (fi == i) continue;
    buc[fi] = 1;
  }
  int ans = 0;
  for (int i = 1; i <= n + 1; i++) {
    if (buc[i]) {
      int fi = find(i);
      if (v[fi].size() > 1) {
        ans += v[fi].size() + !bool(v[fi].count(mp[a[n + 1]]));
      }
    }
  }
  write(ans - 1 == -1 ? 0 : ans - 1);
  return 0;
}
