#include <bits/stdc++.h>
using namespace std;
int const N = 5e5 + 41;
int const INF = 1e9 + 41;
vector<int> a, b;
int n, x;
map<int, int> bal;
map<int, int> h;
vector<int> bpos[N];
int cntb;
int ans = INF;
set<int> bp;
int bad;
void print(int v) {
  cout << v << endl;
  exit(0);
}
void hashIt() {
  h[x] = 0;
  for (int i = 0; i < n; ++i) {
    if (h.find(a[i]) == h.end()) {
      int v = (int)(h.size());
      h[a[i]] = v;
    }
  }
  for (int i = 0; i < n; ++i) {
    if (h.find(b[i]) == h.end()) {
      int v = (int)(h.size());
      h[b[i]] = v;
    }
  }
}
void read() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int v;
    cin >> v;
    a.push_back(v);
    ++bal[v];
    x ^= v;
  }
  for (int i = 0; i < n; ++i) {
    int v;
    cin >> v;
    b.push_back(v);
    --bal[v];
  }
}
void push() {
  for (int i = 0; i < n; ++i)
    if (b[i] != a[i]) {
      bpos[h[b[i]]].push_back(i);
      ++cntb;
      bp.insert(i);
    }
}
void findBad() {
  ++bal[x];
  for (int i = 0; i < n; ++i) ++bal[a[i]], --bal[b[i]];
  for (auto it : bal) {
    if (it.second != 0) bad = it.first;
  }
}
void solve() {
  read();
  hashIt();
  push();
  findBad();
  if (cntb == 0) {
    print(0);
  }
  int curX = x;
  for (int t = 1; t < N; ++t) {
    int myh = h[curX];
    if ((int)(bpos[myh].size()) > 0) {
      if ((int)(bpos[myh].size()) > 1) {
        if (a[bpos[myh].back()] == bad)
          swap(bpos[myh][(int)(bpos[myh].size()) - 1],
               bpos[myh][(int)(bpos[myh].size()) - 2]);
      }
      curX = a[bpos[myh].back()];
      bp.erase(bpos[myh].back());
      bpos[myh].pop_back();
      --cntb;
    } else {
      assert((int)(bp.size()) > 0);
      int fp = (*bp.begin());
      int tmp = a[fp];
      a[fp] = curX;
      curX = tmp;
    }
    if (cntb == 0) {
      ans = t;
      break;
    }
  }
  if (ans == INF) print(-1);
  print(ans);
}
int main() {
  solve();
  return 0;
}
