#include <bits/stdc++.h>
using namespace std;
struct node {
  long long int w, id;
} num[100005], ord[100005];
long long int n, sum = 0, ccc = 1, ans = 0;
long long int cmp1[100005], cmp2[100005], orign[100005], pos[100005];
int flag = -1;
set<int> g[100005], s;
map<int, int> mp;
bool cmp(node a, node b) { return a.w < b.w; }
void Judge() {
  sort(num + 1, num + 1 + ccc, cmp);
  sort(ord + 1, ord + 1 + ccc, cmp);
  for (int i = 1; i <= ccc; i++) mp[num[i].w]++;
  mp[sum]++;
  for (int i = 1; i <= ccc; i++) mp[ord[i].w]--;
  int cnt = 0;
  for (int i = 1; i <= ccc; i++) {
    if (mp[num[i].w] != 0) {
      cnt++;
      flag = num[i].w;
    }
  }
  if (cnt > 1) {
    printf("-1\n");
    exit(0);
  }
}
void Bfs(int u) {
  queue<int> q;
  q.push(u);
  s.erase(u);
  while (!q.empty() && s.size()) {
    int tmp = q.front();
    q.pop();
    for (set<int>::iterator it = g[tmp].begin(); it != g[tmp].end(); it++) {
      if (s.count(*it) == 0) continue;
      q.push(*it);
      s.erase(*it);
    }
  }
}
int main(void) {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &cmp1[i]);
    sum ^= cmp1[i];
  }
  for (int j = 1; j <= n; j++) scanf("%lld", &cmp2[j]);
  for (int i = 1; i <= n; i++) {
    if (cmp1[i] == cmp2[i]) continue;
    num[ccc] = (node){cmp1[i], ccc};
    ord[ccc] = (node){cmp2[i], ccc};
    ccc++;
  }
  ccc--;
  Judge();
  if (flag != -1) {
    for (int i = 1; i <= ccc; i++)
      if (ord[i].w == sum) {
        flag = 1;
        break;
      }
    for (int i = 1; i <= ccc; i++) {
      if (num[i].w == flag) num[i].w = sum;
    }
    sort(num + 1, num + 1 + ccc, cmp);
  }
  for (int i = 1; i <= ccc; i++) {
    int now = i;
    g[num[i].id].insert(ord[now].id);
    now++;
    while (ord[now].w == ord[now - 1].w && now < ccc) {
      g[num[i].id].insert(ord[now].id);
      now++;
    }
  }
  for (int i = 1; i <= ccc; i++) s.insert(i);
  while (s.size()) {
    ans++;
    int now = *s.begin();
    Bfs(now);
  }
  ans += ccc;
  if (flag == 1) ans -= 1;
  printf("%lld\n", ans);
  return 0;
}
