#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, a[N], b[N], xxx = 0, ans = 0, cnt = 0, fa[N], c[N], sum = 0, tmp, vis[N],
                   tag[N];
map<long long, int> mp;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return f * x;
}
inline void write(int x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
  return;
}
inline void no() {
  printf("-1\n");
  exit(0);
}
inline int find(int x) { return lower_bound(c + 1, c + c[0] + 1, x) - c; }
inline int getfa(int x) { return (fa[x] == x) ? x : (fa[x] = getfa(fa[x])); }
inline void merge(int x, int y) {
  int fx = getfa(x), fy = getfa(y);
  if (fx == fy) return;
  tag[x] = tag[y] = 1;
  fa[fx] = fy;
  return;
}
int main() {
  n = read();
  for (register int i = (1); i <= (n); i++)
    a[i] = read(), xxx ^= a[i], mp[a[i]]++, sum++;
  mp[xxx]++, sum++;
  for (register int i = (1); i <= (n); i++) {
    b[i] = read();
    if (mp[b[i]]) sum--, mp[b[i]]--;
  }
  if (sum != 1) no();
  for (register int i = (1); i <= (n); i++) c[++c[0]] = a[i], c[++c[0]] = b[i];
  sort(c + 1, c + c[0] + 1);
  c[0] = unique(c + 1, c + c[0] + 1) - c - 1;
  for (register int i = (1); i <= (c[0]); i++) fa[i] = i;
  for (register int i = (1); i <= (n); i++)
    if (a[i] != b[i]) {
      ans++;
      merge(find(a[i]), find(b[i]));
    }
  for (register int i = (1); i <= (n); i++)
    if (a[i] != b[i]) {
      tmp = getfa(find(a[i]));
      if (!vis[tmp]) vis[tmp] = 1, cnt++;
    }
  ans += cnt - 1;
  if (ans && !tag[find(xxx)]) ans++;
  write(ans);
  return 0;
}
