#include <bits/stdc++.h>
using namespace std;
map<int, int> mp, s;
int n, a[100005], b[100005], cnt;
int f[100005], siz[100005], ss[100005];
int find(int x) {
  if (x == f[x]) return x;
  return f[x] = find(f[x]);
}
void add(int a, int b) {
  int aa = find(a), bb = find(b);
  if (aa != bb) {
    f[aa] = bb;
    siz[bb] += siz[aa];
    ss[bb] += ss[aa];
  }
  ss[bb]++;
}
int main() {
  scanf("%d", &n);
  int sum = 0, fl = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    sum ^= a[i];
    mp[a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    if (s[a[i]] != 0) continue;
    s[a[i]] = ++cnt;
    f[cnt] = cnt;
    siz[cnt] = mp[a[i]];
  }
  if (mp.find(sum) != mp.end()) mp[sum]++;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    if (b[i] == a[i]) siz[find(s[a[i]])]--;
    if (mp.find(b[i]) == mp.end()) {
      if (!fl && b[i] == sum)
        fl = 1;
      else
        return puts("-1"), 0;
    } else {
      if (!fl && b[i] == sum)
        fl = 1;
      else if (mp[b[i]] == 0)
        return puts("-1"), 0;
      if (mp[b[i]] != 0) mp[b[i]]--, add(s[b[i]], s[a[i]]);
    }
  }
  int ans = 0;
  for (int i = 1; i <= cnt; i++) {
    if (find(i) == i) {
      if (siz[i] == 0) continue;
      ans += siz[i] + 1;
    }
  }
  if (fl) ans--;
  cout << ans << endl;
  return 0;
}
