#include <bits/stdc++.h>
const int N = 200005;
using namespace std;
int fa[N], tot = 0, dif = 0, sam = 0, ans = 0;
int n, num[N], a[N], b[N], sz[N], cnt = 0;
map<int, int> vis;
int getfa(int x) {
  if (fa[x] != x) fa[x] = getfa(fa[x]);
  return fa[x];
}
void add(int x, int y) {
  int fx = getfa(x), fy = getfa(y);
  if (fx != fy) fa[fx] = fy;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (!vis[a[i]]) cnt++, vis[a[i]] = cnt;
    tot ^= a[i];
    num[vis[a[i]]]++;
  }
  if (!vis[tot]) cnt++, vis[tot] = cnt;
  num[vis[tot]]++;
  for (int i = 1; i <= cnt; i++) fa[i] = i, sz[i] = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    if (!vis[b[i]] || num[vis[b[i]]] <= 0) ans = -1;
    num[vis[b[i]]]--;
    if (vis[a[i]] == vis[b[i]]) {
      sam++;
      continue;
    }
    add(vis[a[i]], vis[b[i]]);
  }
  if (ans == -1) {
    printf("-1\n");
    return 0;
  }
  ans = n - sam;
  for (int i = 1; i <= cnt; i++) {
    if (num[i]) {
      add(i, vis[tot]);
      if (i != vis[tot]) ans--;
      break;
    }
  }
  for (int i = 1; i <= cnt; i++) fa[i] = getfa(i);
  for (int i = 1; i <= cnt; i++) sz[fa[i]]++;
  for (int i = 1; i <= cnt; i++) {
    if (fa[i] == i && sz[i] != 1) ans++;
  }
  printf("%d\n", ans);
  return 0;
}
