#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
  x = 0;
  static int p;
  p = 1;
  static char c;
  c = getchar();
  while (!isdigit(c)) {
    if (c == '-') p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - '0');
    c = getchar();
  }
  x *= p;
}
int n;
int A[100010], B[100010];
set<pair<int, int> > s;
map<int, int> mp;
int main() {
  read(n);
  int sum = 0, pd = 0;
  for (int i = 1; i <= n; i++) read(A[i]), sum ^= A[i], mp[A[i]]++;
  for (int i = 1; i <= n; i++) {
    read(B[i]);
    if (A[i] != B[i]) s.insert(pair<int, int>(B[i], i));
    if (!mp.count(B[i])) {
      if (B[i] != sum)
        return puts("-1"), 0;
      else if (!pd)
        pd = 1;
      else
        return puts("-1"), 0;
    } else {
      mp[B[i]]--;
      if (!mp[B[i]]) mp.erase(B[i]);
    }
  }
  int ans = 0, stsum = 0;
  set<pair<int, int> >::iterator it, itt;
  stsum = sum;
  while (!s.empty()) {
    it = s.lower_bound(pair<int, int>(stsum, 0));
    {
      if (it == s.end()) it = s.begin();
      swap(stsum, A[(*it).second]);
      ans++;
      if ((*it).first == A[(*it).second]) s.erase(it);
    }
  }
  printf("%d\n", ans);
  return 0;
}
