#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int pa[maxn], a[maxn], b[maxn], xx[maxn], f[maxn];
int n, cnt;
map<int, int> c;
int del(int n, int x[maxn]) {
  int cnt = 1;
  for (int i = (2); i <= (n); i++)
    if (x[i] != x[cnt]) x[++cnt] = x[i];
  return cnt;
}
int getpa(int x) { return pa[x] == x ? x : pa[x] = getpa(pa[x]); }
int main() {
  scanf("%d", &n);
  n++;
  for (int i = (1); i <= (n - 1); i++) {
    scanf("%d", &a[i]);
    a[n] ^= a[i];
    c[a[i]]++;
    xx[++cnt] = a[i];
  }
  c[a[n]]++;
  xx[++cnt] = a[n];
  for (int i = (1); i <= (n - 1); i++) {
    scanf("%d", &b[i]);
    b[n] ^= b[i];
    c[b[i]]--;
  }
  c[b[n]]--;
  for (map<int, int>::iterator it = c.begin(); it != c.end(); it++)
    if (it->second != 0) return puts("-1"), 0;
  sort(xx + 1, xx + cnt + 1);
  cnt = del(cnt, xx);
  int ans = n;
  for (int i = (1); i <= (cnt); i++) pa[i] = i, f[i] = 0;
  for (int i = (1); i <= (n); i++) {
    a[i] = lower_bound(xx + 1, xx + cnt + 1, a[i]) - xx;
    b[i] = lower_bound(xx + 1, xx + cnt + 1, b[i]) - xx;
    if (getpa(a[i]) != getpa(b[i])) {
      pa[getpa(a[i])] = getpa(b[i]);
      f[getpa(b[i])] = 1;
    }
    if (a[i] == b[i]) ans--;
  }
  if (a[n] != b[n]) ans -= 2;
  for (int i = (1); i <= (cnt); i++)
    if (pa[i] == i) ans += f[i];
  cout << ans << endl;
  return 0;
}
