#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int n = 0, f = 1;
  char c;
  for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar())
    ;
  if (c == '-') c = getchar(), f = -1;
  for (; c >= '0' && c <= '9'; c = getchar()) n = n * 10 + c - 48;
  return n * f;
}
const int maxn = 1e5 + 5;
int i, j, n, a[maxn], b[maxn], x, ans;
bool p;
int f[maxn], cnt;
bool bz[maxn];
map<int, int> d;
map<int, int> c;
int get(int x) { return (f[x]) ? f[x] = get(f[x]) : x; }
void hb(int x, int y) {
  x = get(d[x]), y = get(d[y]);
  if (x != y) f[x] = y;
}
int main() {
  n = read();
  for (i = 1; i <= n; i++) x ^= (a[i] = read()), c[a[i]]++;
  c[x]++;
  for (i = 1; i <= n; i++) {
    b[i] = read();
    if (!d[b[i]]) d[b[i]] = ++cnt;
    if (c[b[i]])
      c[b[i]]--;
    else
      return puts("-1"), 0;
  }
  for (i = 1; i <= n; i++) hb(a[i], b[i]), p |= b[i] == x, ans += a[i] != b[i];
  for (i = 1; i <= n; i++)
    if (a[i] != b[i]) ans += !bz[get(d[a[i]])], bz[get(d[a[i]])] = 1;
  printf("%d\n", ans - p);
}
