#include <bits/stdc++.h>
using namespace std;
int A[100000], B[100000];
map<int, int> fra;
map<int, vector<int> > b;
int main() {
  int n;
  scanf("%d", &n);
  int x = 0;
  for (int i = 0; i < (int)(n); ++i) scanf("%d", A + i), x ^= A[i], ++fra[A[i]];
  int ok = 0;
  for (int i = 0; i < (int)(n); ++i) {
    scanf("%d", B + i);
    if (B[i] == A[i])
      ++ok, --fra[A[i]];
    else
      b[B[i]].push_back(i);
  }
  ++fra[x];
  for (map<int, vector<int> >::iterator it = b.begin(); it != b.end(); ++it)
    if (fra[it->first] < (int)it->second.size()) {
      printf("-1\n");
      return 0;
    }
  int an = 0;
  while (ok < n) {
    ++an;
    map<int, vector<int> >::iterator it = b.find(x);
    bool g = false;
    if (it == b.end())
      it = b.begin();
    else
      g = true;
    swap(x, A[it->second.back()]);
    if (g) {
      ++ok;
      it->second.pop_back();
      if (it->second.empty()) b.erase(it);
    }
  }
  printf("%d\n", an);
}
