#include <bits/stdc++.h>
using namespace std;
template <class T>
using vv = vector<vector<T>>;
template <class T>
inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T>
inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}
const long long MOD = 1e9 + 7;
int N;
int A[114514];
int B[114514];
map<int, queue<int>> es;
int main() {
  scanf("%d", &N);
  int x = 0;
  for (int(i) = 0; (i) < (N); ++(i)) {
    scanf("%d", &A[i]);
    x ^= A[i];
  }
  for (int(i) = 0; (i) < (N); ++(i)) {
    scanf("%d", &B[i]);
    if (A[i] != B[i]) es[B[i]].push(A[i]);
  }
  int defx = x;
  int ans = 0;
  while (!es.empty()) {
    if (!es.count(x)) {
      assert(!es.empty());
      ans++;
      int want = es.begin()->first;
      int give;
      while (1) {
        give = es[want].front();
        es[want].pop();
        if (give != defx || es[want].empty()) {
          break;
        }
        es[want].push(give);
      }
      es[want].push(x);
      x = give;
    }
    if (!es.count(x)) {
      puts("-1");
      return 0;
    }
    ans++;
    int give;
    while (1) {
      give = es[x].front();
      es[x].pop();
      if (give != defx || es[x].empty()) {
        break;
      }
      es[x].push(give);
    }
    if (es[x].empty()) es.erase(x);
    x = give;
  }
  printf("%d\n", ans);
}
