#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 8;
struct Node {
  int x, p;
} c[N], d[N];
int a[N], b[N], to[N], cnt, tot, g[N], s, n;
bool flag = false, vis[N];
bool cmp(Node a, Node b) { return a.x < b.x; }
bool check() {
  for (int i = 1; i <= cnt; i++) {
    c[i].x = a[g[i]], d[i].x = b[g[i]];
    c[i].p = g[i], d[i].p = g[i];
  }
  sort(c + 1, c + cnt + 1, cmp);
  sort(d + 1, d + cnt + 1, cmp);
  int x = 0;
  int posi = 0;
  for (int i = 1; i <= cnt; i++)
    if (c[i + x].x != d[i].x) {
      if (d[i].x == s && !flag)
        flag = true, x = -1, posi = i;
      else
        return puts("-1"), 0;
    } else
      to[c[i + x].p] = d[i].p;
  if (flag) to[c[cnt].p] = d[posi].p;
  return true;
}
void dfs(int x) {
  if (vis[x]) return;
  vis[x] = true;
  dfs(to[x]);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    s = s ^ a[i];
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    if (a[i] != b[i]) g[++cnt] = i;
  }
  if (!check()) return 0;
  for (int i = 1; i <= n; i++)
    if (!vis[i] && a[i] != b[i]) tot++, dfs(i);
  if (flag) cnt--;
  printf("%d", cnt + tot);
  return 0;
}
