#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
void read(int &x) {
  x = 0;
  char ch = getchar();
  int f = 1;
  while (ch < '0' || ch > '9') ch == '-' ? f = -1 : 0, ch = getchar();
  while ('0' <= ch && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  x *= f;
  return;
}
int n, a[N], b[N], c[N], d[N];
int tmp, cnt;
int zzk;
int check(int x) {
  return (upper_bound(d + 1, d + 1 + n, x) - d) -
             (lower_bound(d + 1, d + 1 + n, x) - d) >
         0;
}
struct xx {
  int V, next;
} B[N << 2];
int head[N], bel[N], G;
void add(int x, int y) {
  B[++G] = (xx){y, head[x]};
  head[x] = G;
}
int dfn[N], low[N], T, In[N], st[N];
void dfs(int x) {
  low[x] = dfn[x] = ++T;
  st[++st[0]] = x;
  In[x] = 1;
  for (int i = head[x]; i; i = B[i].next)
    if (!dfn[B[i].V])
      dfs(B[i].V), low[x] = min(low[x], low[B[i].V]);
    else if (In[B[i].V])
      low[x] = min(low[x], dfn[B[i].V]);
  if (dfn[x] == low[x]) {
    for (++cnt; st[st[0]] ^ x; --st[0]) In[st[st[0]]] = 0, bel[st[st[0]]] = cnt;
    bel[x] = cnt, --st[0], In[x] = 0;
  }
  return;
}
int ans;
map<int, int> ma, mb;
int main() {
  read(n);
  for (int i = 1; i <= n; ++i) read(a[i]), ++ma[a[i]], tmp ^= a[i], c[i] = a[i];
  c[n + 1] = a[n + 1] = tmp;
  sort(c + 1, c + 1 + n + 1);
  tmp = 0;
  for (int i = 1; i <= n; ++i) read(b[i]), ++mb[b[i]], tmp ^= b[i], d[i] = b[i];
  b[n + 1] = d[n + 1] = tmp;
  sort(d + 1, d + 1 + n + 1);
  for (map<int, int>::iterator i = ma.begin(); i != ma.end(); ++i)
    if ((*i).second < mb[(*i).first]) return printf("-1"), 0;
  ;
  zzk = unique(c + 1, c + 1 + n + 1) - c;
  tmp = lower_bound(c + 1, c + zzk, tmp) - c;
  for (int i = 1; i <= n + 1; ++i) a[i] = lower_bound(c + 1, c + zzk, a[i]) - c;
  for (int i = 1; i <= n; ++i) b[i] = lower_bound(c + 1, c + zzk, b[i]) - c;
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i]) add(a[i], b[i]);
  cnt = 0;
  for (int i = 1; i < zzk; ++i)
    if (head[i] && !bel[i]) dfs(i);
  ans = max(0, cnt - 1);
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i] && bel[a[i]] == bel[b[i]]) ++ans;
  return printf("%d", ans), 0;
}
