#include <bits/stdc++.h>
using namespace std;
inline int read(int f = 1, int x = 0, char ch = ' ') {
  while (!isdigit(ch = getchar()))
    if (ch == '-') f = -1;
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  return f * x;
}
const int N = 1e5 + 5;
int n, a[N], b[N], c[N], d[N], ans;
set<int> s, f[N];
int main() {
  n = read(), ++n;
  for (int i = 1; i < n; ++i) c[i] = a[i] = read(), c[n] = a[0] ^= a[i];
  for (int i = 1; i < n; ++i) d[i] = b[i] = read(), d[n] = b[0] ^= b[i];
  sort(c + 1, c + 1 + n), sort(d + 1, d + 1 + n);
  for (int i = 1; i <= n; ++i)
    if (c[i] ^ d[i]) return puts("-1"), 0;
  for (int i = 0; i < n; ++i)
    a[i] = lower_bound(c + 1, c + 1 + n, a[i]) - c,
    b[i] = lower_bound(d + 1, d + 1 + n, b[i]) - d;
  --n;
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i]) s.insert(i), f[b[i]].insert(i);
  for (int k = 0; s.size(); ++ans)
    if (f[a[0]].size())
      k = *(--f[a[0]].end()), s.erase(k), f[a[0]].erase(k), swap(a[k], a[0]);
    else
      swap(a[0], a[*s.begin()]);
  printf("%d\n", ans);
  return 0;
}
