#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
inline int read() {
  int x = 0;
  char ch = getchar();
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  return x;
}
int n, a[maxn], b[maxn], x = 0, to[maxn], ca[maxn], cb[maxn], N, bel[maxn],
                         ans = 0, id[maxn], lst[maxn];
vector<int> pos[maxn], vec;
int find(int x) {
  if (bel[x] == x) return x;
  return bel[x] = find(bel[x]);
}
inline void unite(int x, int y) {
  x = find(x), y = find(y);
  bel[x] = y;
}
int main() {
  n = read();
  for (int i = 1; i <= n; ++i) bel[i] = i;
  for (int i = 1; i <= n; ++i) a[i] = read(), x ^= a[i];
  for (int i = 1; i <= n; ++i) b[i] = read();
  vec.push_back(x);
  for (int i = 1; i <= n; ++i) {
    vec.push_back(a[i]);
    vec.push_back(b[i]);
  }
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  N = vec.size();
  x = lower_bound(vec.begin(), vec.end(), x) - vec.begin();
  for (int i = 1; i <= n; ++i) {
    a[i] = lower_bound(vec.begin(), vec.end(), a[i]) - vec.begin();
    b[i] = lower_bound(vec.begin(), vec.end(), b[i]) - vec.begin();
    ++ca[a[i]];
    ++cb[b[i]];
  }
  bool flag = 1;
  for (int i = 0; i < N; ++i)
    if (ca[i] != cb[i]) flag = false;
  if (flag) {
    for (int i = 1; i <= n; ++i) {
      if (a[i] == b[i]) continue;
      pos[b[i]].push_back(i);
    }
    for (int i = 1; i <= n; ++i) {
      if (a[i] == b[i]) continue;
      if (a[i] == x) ans = -1;
      to[i] = pos[a[i]][id[a[i]]++];
      unite(i, to[i]);
      if (lst[a[i]]) unite(i, lst[a[i]]);
      lst[a[i]] = i;
    }
    for (int i = 1; i <= n; ++i) {
      if (a[i] == b[i]) continue;
      ++ans;
      if (find(i) == i) ++ans;
    }
    printf("%d\n", ans);
  } else {
    ++ca[x];
    for (int i = 0; i < N; ++i)
      if (ca[i] < cb[i]) {
        puts("-1");
        return 0;
      }
    int t;
    ans = -1;
    for (int i = 0; i < N; ++i)
      if (ca[i] > cb[i]) t = i;
    bool vis = false;
    for (int i = 1; i <= n; ++i) {
      if (a[i] == b[i]) continue;
      if (a[i] == t && !vis) {
        vis = true;
        swap(x, a[i]);
      }
      pos[b[i]].push_back(i);
    }
    for (int i = 1; i <= n; ++i) {
      if (a[i] == b[i]) continue;
      to[i] = pos[a[i]][id[a[i]]++];
      unite(i, to[i]);
      if (lst[a[i]]) unite(i, lst[a[i]]);
      lst[a[i]] = i;
    }
    for (int i = 1; i <= n; ++i) {
      if (a[i] == b[i]) continue;
      ++ans;
      if (find(i) == i) ++ans;
    }
    printf("%d\n", ans);
  }
  return 0;
}
