#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
multiset<int> A, B;
int n, a[N], b[N], c[N];
int Rank(int x) {
  int l = 0, r = n;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (c[mid] >= x)
      r = mid;
    else
      l = mid + 1;
  }
  return l;
}
set<int> S[N], T;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    a[0] ^= a[i];
  }
  for (int i = 0; i <= n; i++) A.insert(a[i]), c[i] = a[i];
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    b[0] ^= b[i];
  }
  for (int i = 0; i <= n; i++) B.insert(b[i]);
  if (A != B) return puts("-1"), 0;
  sort(c, c + n + 1);
  for (int i = 0; i <= n; i++) {
    a[i] = Rank(a[i]);
    b[i] = Rank(b[i]);
    if (i && a[i] != b[i]) {
      S[b[i]].insert(i);
      T.insert(i);
    }
  }
  int ans = 0;
  while (!T.empty()) {
    while (!S[a[0]].empty()) {
      int p = *S[a[0]].begin();
      S[a[0]].erase(p);
      T.erase(p);
      swap(a[0], a[p]);
      ans++;
    }
    if (T.empty()) break;
    int p = *T.begin();
    swap(a[0], a[p]);
    ans++;
  }
  printf("%d\n", ans);
  return 0;
}
