#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, gg = 23333;
struct EDGE {
  int to, nt;
} ed[N << 1];
struct data {
  int v, id;
} a[N], b[N];
map<int, int> qx;
int g[N], vis[N], js[N], head[N], pa[N];
int n, num = 0, cnt = 0, sum = 0, res = 0, ans = 0, flag = 0;
void read(int &x) {
  x = 0;
  int f;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar()) f |= (ch == '-');
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ 48);
}
void add(int x, int y) {
  ed[++cnt].to = y, ed[cnt].nt = head[x], head[x] = cnt;
}
void dfs(int x, int y) {
  vis[x] = 1;
  g[x] = y;
  for (int i = head[x]; i; i = ed[i].nt) {
    if (vis[ed[i].to]) {
      int tt = ed[i].to, pos = x;
      ++num;
      for (; pos != tt; pos = g[pos]) pa[num]++;
      pa[num]++;
    } else
      dfs(ed[i].to, x);
  }
}
bool comp(data xx, data yy) {
  if (xx.v != yy.v)
    return xx.v < yy.v;
  else
    return xx.id < yy.id;
}
void GG() {
  while (1) {
    for (int i = 1; i <= n; ++i) {
      if (b[i].v == sum && a[i].v != b[i].v) {
        int temp = sum;
        sum = a[i].v;
        a[i].v = temp;
        ans++;
      }
    }
    int ok = 0;
    for (int i = 1; i <= n; ++i) {
      if (a[i].v != b[i].v) ok++;
    }
    if (!ok) {
      printf("%d\n", ans);
      exit(0);
    }
  }
}
void fail() {
  printf("-1\n");
  exit(0);
}
int main() {
  read(n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i].v), a[i].id = i, sum ^= a[i].v;
  for (int i = 1; i <= n; ++i)
    scanf("%d", &b[i].v), b[i].id = i, qx[b[i].v] = i;
  for (int i = 1; i <= n; ++i) {
    if (!qx.count(a[i].v)) {
      if (flag) fail();
      flag = 1;
    }
    if (!qx.count(a[i].v) && !qx.count(sum)) fail();
  }
  if (n <= 5000) GG();
  if (flag) {
    for (int i = 1; i <= n; ++i) {
      if (b[i].v == sum && b[i].v != a[i].v) {
        int temp = sum;
        sum = a[i].v;
        a[i].v = sum;
        ans++;
      }
    }
  }
  sort(a + 1, a + 1 + n, comp);
  sort(b + 1, b + 1 + n, comp);
  for (int i = 1; i <= n; ++i) {
    if (a[i].v != b[i].v) {
      js[++res] = a[i].id, add(a[i].id, n + 1);
    } else if (a[i].id != b[i].id) {
      js[++res] = a[i].id;
      add(a[i].id, b[i].id);
    }
  }
  for (int i = 1; i <= res; ++i)
    if (!vis[js[i]]) dfs(js[i], js[i]);
  for (int i = 1; i <= num; ++i) ans += pa[i] + 1;
  printf("%d\n", ans);
  return 0;
}
