#include <bits/stdc++.h>
using namespace std;
const int NR = 1e5 + 100;
int n, a[NR], b[NR], sa, sb, c[NR], d[NR];
multimap<int, int> pb;
map<int, int> fa;
int gf(int x) {
  if (fa.find(x) == fa.end()) return fa[x] = x;
  return fa[x] == x ? x : (fa[x] = gf(fa[x]));
}
int main() {
  scanf("%d", &n);
  int sa = 0, sb = 0;
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), sa ^= a[i], c[i] = a[i];
  for (int i = 1; i <= n; ++i) scanf("%d", &b[i]), sb ^= b[i], d[i] = b[i];
  c[n + 1] = sa;
  d[n + 1] = sb;
  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]) {
      puts("-1");
      return 0;
    }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] != b[i]) {
      ans += 1;
      int fx = gf(a[i]), fy = gf(b[i]);
      if (fx != fy) fa[fx] = fy;
    }
  }
  --ans;
  for (auto p : fa)
    if (p.first == p.second) ++ans;
  printf("%d\n", ans);
  return 0;
}
