#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, a[N], b[N];
map<int, int> p;
inline int bel(int i) { return i == p[i] ? i : p[i] = bel(p[i]); }
int main() {
  scanf("%d", &n);
  multiset<int> c, d;
  for (int i = 1; i <= n; ++i) scanf("%d", a + i), a[0] ^= a[i], c.insert(a[i]);
  c.insert(a[0]);
  for (int i = 1; i <= n; ++i) scanf("%d", b + i), b[0] ^= b[i], d.insert(b[i]);
  d.insert(b[0]);
  if (c != d) return !~puts("-1");
  for (int i = 0; i <= n; ++i) p[a[i]] = a[i];
  int s = -1;
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i]) p[bel(a[i])] = bel(b[i]), ++s;
  for (int i = 0; i <= n; ++i)
    if ((!i || a[i] != b[i]) && a[i] == p[a[i]]) ++s;
  printf("%d\n", s);
}
