#include <bits/stdc++.h>
using namespace std;
int a[1000500], b[1000500], c[1000500], d[1000500], fa[1000500], n, ans, tot;
map<int, int> mp;
int gf(int x) { return fa[x] == x ? x : fa[x] = gf(fa[x]); }
inline int rd() {
  int r;
  scanf("%d", &r);
  return r;
}
int main() {
  n = rd();
  for (int _ = 1; _ <= n; _++) a[_] = rd();
  for (int _ = 1; _ <= n; _++) b[_] = rd();
  for (int _ = 1; _ <= n; _++) a[n + 1] ^= a[_], b[n + 1] ^= b[_];
  ++n;
  for (int _ = 1; _ <= n; _++) c[_] = a[_], d[_] = b[_];
  sort(c + 1, c + n + 1);
  sort(d + 1, d + n + 1);
  for (int _ = 1; _ <= n; _++)
    if (d[_] != c[_]) return puts("-1"), 0;
  for (int _ = 1; _ <= n; _++)
    if (a[_] != b[_]) {
      _ < n ? ++ans : 0;
      !mp[a[_]] ? mp[a[_]] = ++tot : 0;
      !mp[b[_]] ? mp[b[_]] = ++tot : 0;
    }
  if (!ans) return puts("0"), 0;
  for (int _ = 1; _ <= tot; _++) fa[_] = _;
  for (int _ = 1; _ < n; _++)
    if (a[_] != b[_]) fa[gf(mp[a[_]])] = gf(mp[b[_]]);
  for (int _ = 1; _ <= tot; _++) ans += fa[_] == _;
  cout << ans - 1 << endl;
  return 0;
}
