#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
map<int, int> qx;
int n, a[N], b[N], c[N], f[N], vis[N];
void read(int &x) {
  x = 0;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar()) 23333;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ 48);
}
int find(int k) { return f[k] == k ? k : find(f[k]); }
void add(int x, int y) {
  int xx = find(x), yy = find(y);
  if (xx != yy) {
    f[xx] = yy;
    vis[yy] = 1;
  }
}
void fail() {
  printf("-1\n");
  exit(0);
}
int main() {
  read(n);
  for (int i = 1; i <= n; ++i) {
    read(a[i]);
    qx[a[i]]++;
    a[n + 1] ^= a[i];
  }
  qx[a[n + 1]]++;
  for (int i = 1; i <= n; ++i) {
    read(b[i]);
    qx[b[i]]--;
    if (qx[b[i]] < 0) fail();
    b[n + 1] ^= b[i];
  }
  ++n;
  for (int i = 1; i <= n; ++i) c[i] = a[i];
  int ans = 0;
  sort(c + 1, c + 1 + n);
  int m = unique(c + 1, c + 1 + n) - c - 1;
  for (int i = 1; i <= n; ++i) a[i] = lower_bound(c + 1, c + 1 + m, a[i]) - c;
  for (int i = 1; i <= n; ++i) b[i] = lower_bound(c + 1, c + 1 + m, b[i]) - c;
  for (int i = 1; i <= n; ++i) f[i] = i;
  for (int i = 1; i <= n; ++i) {
    if (a[i] != b[i]) {
      add(a[i], b[i]);
      if (i < n) ans++;
    }
  }
  if (!ans) return printf("0\n"), 0;
  for (int i = 1; i <= n; ++i)
    if (f[i] == i && vis[i]) ans++;
  ans--;
  printf("%d\n", ans);
  return 0;
}
