#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int vis[N], ft[N], n, ans, sa, sb, tot, a[N], b[N], cnt[N];
map<int, int> mp;
map<int, int>::iterator it;
struct edge {
  int t, n;
} E[N << 2];
void dfs(int u) {
  vis[u] = 1;
  for (int o = ft[u], v; v = E[o].t; o = E[o].n)
    if (!vis[v]) dfs(v);
}
void add(int u, int v) { E[++tot] = (edge){v, ft[u]}, ft[u] = tot; }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", a + i), sa ^= a[i], mp[a[i]];
  for (int i = 1; i <= n; i++) scanf("%d", b + i), sb ^= b[i], mp[b[i]];
  mp[sa], mp[sb];
  int __ = 0;
  for (it = mp.begin(); it != mp.end(); it++) it->second = ++__;
  for (int i = 1; i <= n; i++) a[i] = mp[a[i]], b[i] = mp[b[i]];
  sa = mp[sa], sb = mp[sb];
  add(sa, sb), add(sb, sa);
  for (int i = 1; i <= n; i++)
    add(a[i], b[i]), add(b[i], a[i]), ans -= a[i] == b[i];
  ans += n;
  for (int i = 1; i <= n; i++) ++cnt[a[i]];
  if (cnt[sb]) ans--;
  for (int i = 1; i <= n; i++) --cnt[b[i]];
  ++cnt[sa], --cnt[sb];
  for (int i = 1; i <= n * 2 + 3; i++)
    if (cnt[i]) return puts("-1"), 0;
  for (int i = 1; i <= __; i++)
    if (!vis[i]) {
      for (int o = ft[i], v; v = E[o].t; o = E[o].n)
        if (v != i) goto cc;
      continue;
    cc:
      ++ans;
      dfs(i);
    }
  printf("%d\n", ans);
  return 0;
}
