#include <bits/stdc++.h>
using namespace std;
int ls[200004], cnt, n, a[100002], b[100002], head[200002], tot, ans,
    vis[200002], t1[200002], t2[200002];
struct edge {
  int a, b, next;
} e[800002];
void add(int a, int b) {
  e[++tot].a = a, e[tot].b = b, e[tot].next = head[a], head[a] = tot;
}
void dfs(int k) {
  vis[k] = 1;
  for (int i = head[k]; i != 0; i = e[i].next) {
    if (!vis[e[i].b]) dfs(e[i].b);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    ls[++cnt] = a[i];
    a[n + 1] ^= a[i];
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    ls[++cnt] = b[i];
    b[n + 1] ^= b[i];
  }
  ls[++cnt] = a[n + 1], ls[++cnt] = b[n + 1];
  sort(ls + 1, ls + 1 + cnt);
  cnt = unique(ls + 1, ls + 1 + cnt) - ls - 1;
  for (int i = 1; i <= n + 1; i++)
    a[i] = lower_bound(ls + 1, ls + 1 + cnt, a[i]) - ls;
  for (int i = 1; i <= n + 1; i++)
    b[i] = lower_bound(ls + 1, ls + 1 + cnt, b[i]) - ls;
  for (int i = 1; i <= n + 1; i++) t1[i] = a[i], t2[i] = b[i];
  sort(t1 + 1, t1 + n + 2);
  sort(t2 + 1, t2 + n + 2);
  for (int i = 1; i <= n + 1; i++)
    if (t2[i] != t1[i]) {
      printf("-1");
      return 0;
    }
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) {
      add(a[i], b[i]);
      ans++;
    }
  if (a[n + 1] != b[n + 1]) add(a[n + 1], b[n + 1]);
  int ban = b[n + 1];
  for (int i = 1; i <= cnt; i++)
    if ((!vis[i]) && head[i] && (i != ban)) {
      dfs(i);
      ans++;
    }
  if (vis[ban] == 0) ans++;
  if (ans == 1) {
    printf("");
    return 0;
  }
  printf("%d\n", ans - 1);
  return 0;
}
