#include <bits/stdc++.h>
using namespace std;
int n;
const int maxn = 2e5 + 5;
int a[maxn], b[maxn];
int cnt[maxn];
map<int, int> ID;
int tot;
int fa[maxn], sz[maxn];
bool vis[maxn];
int Find(int x) { return x == fa[x] ? x : fa[x] = Find(fa[x]); }
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  int x = 0;
  tot = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (!ID.count(a[i])) {
      ID[a[i]] = ++tot;
    }
    x ^= a[i];
    cnt[ID[a[i]]]++;
    a[i] = ID[a[i]];
  }
  int diff = 0;
  int di = -1;
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    if (!ID.count(b[i])) {
      diff++;
      di = b[i];
      ID[b[i]] = ++tot;
      b[i] = ID[b[i]];
      continue;
    }
    if (cnt[ID[b[i]]] == 0) {
      diff++;
      di = b[i];
    } else {
      cnt[ID[b[i]]]--;
    }
    b[i] = ID[b[i]];
  }
  if (diff > 1) {
    cout << "-1" << endl;
  } else {
    for (int i = 1; i <= n; i++) {
      fa[a[i]] = a[i];
      fa[b[i]] = b[i];
      sz[a[i]]++;
    }
    for (int i = 1; i <= n; i++) {
      if (a[i] != b[i]) {
        int fx = Find(a[i]);
        int fy = Find(b[i]);
        if (fx != fy) {
          fa[fx] = fy;
          sz[fy] += sz[fx];
        }
      }
    }
    if (diff == 1 and di != x) {
      cout << "-1" << endl;
    } else {
      int ans = 0;
      for (int i = 1; i <= n; i++) {
        if (a[i] == b[i]) continue;
        if (b[i] == ID[x]) {
          int fx = Find(a[i]);
          if (!vis[fx]) {
            vis[fx] = 1;
            ans += -1;
          }
        }
      }
      memset(vis, 0, sizeof(vis));
      for (int i = 1; i <= n; i++) {
        if (a[i] == b[i]) continue;
        int fx = Find(a[i]);
        if (!vis[fx]) {
          vis[fx] = 1;
          ans += sz[fx] + 1;
        }
      }
      cout << ans << endl;
    }
  }
  return 0;
}
