#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int a[maxn], b[maxn], pa[maxn], sz[maxn];
int n, ans, flag = 1, cnt;
multiset<int> s;
map<int, int> M;
inline int getpa(int x) { return pa[x] == x ? x : pa[x] = getpa(pa[x]); }
int main() {
  scanf("%d", &n);
  for (int i = (1); i <= (n); i++) {
    scanf("%d", &a[i]);
    a[n + 1] ^= a[i];
  }
  for (int i = (1); i <= (n); i++) {
    scanf("%d", &b[i]);
    b[n + 1] ^= b[i];
  }
  n++;
  for (int i = (1); i <= (n); i++) s.insert(a[i]);
  for (int i = (1); i <= (n); i++) {
    if (s.find(b[i]) == s.end()) return puts("-1"), 0;
    s.erase(s.find(b[i]));
  }
  for (int i = (1); i <= (n); i++)
    if (!M[a[i]]) M[a[i]] = ++cnt;
  for (int i = (1); i <= (n); i++) a[i] = M[a[i]];
  for (int i = (1); i <= (n); i++) b[i] = M[b[i]];
  for (int i = (1); i <= (cnt); i++) pa[i] = i, sz[i] = 1;
  for (int i = (1); i <= (n); i++)
    if (i == n || a[i] != b[i]) {
      a[i] = getpa(a[i]);
      b[i] = getpa(b[i]);
      if (a[i] != b[i]) {
        pa[a[i]] = b[i];
        sz[b[i]] += sz[a[i]];
      }
      if (i < n) ans++;
    }
  for (int i = (1); i <= (cnt); i++)
    if (pa[i] == i && sz[i] >= 2) ans++;
  printf("%d\n", ans - 1);
  return 0;
}
