#include <bits/stdc++.h>
using namespace std;
map<int, int> f;
int Find(int first) {
  if (f.find(first) == f.end()) return f[first] = first;
  if (f[first] == first) return first;
  return f[first] = Find(f[first]);
}
int main() {
  int n;
  scanf("%d", &n);
  int a[100005];
  int b[100005];
  int c[100005];
  int d[100005];
  a[n + 1] = b[n + 1] = c[n + 1] = d[n + 1] = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    c[i] = a[i];
    c[n + 1] ^= a[i];
    a[n + 1] ^= a[i];
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    d[i] = b[i];
    d[n + 1] ^= b[i];
    b[n + 1] ^= b[i];
  }
  sort(c + 1, c + n + 2);
  sort(d + 1, d + n + 2);
  for (int i = 1; i <= n + 1; i++) {
    if (c[i] != d[i]) {
      printf("-1\n");
      return 0;
    }
  }
  int ans = 0;
  int ok = 0;
  for (int i = 1; i <= n + 1; i++) {
    if (a[i] != b[i]) {
      int fa = Find(a[i]), fb = Find(b[i]);
      f[fa] = fb;
      ans++;
    }
  }
  int fx = Find(a[n + 1]);
  for (auto it : f) {
    if (it.first == it.second) ans++;
  }
  printf("%d\n", ans - 1);
}
