#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int INF = 2000000000;
const long long BIG = 1446803456761533460LL;
const int maxn = 100010;
int n, A[maxn], B[maxn];
map<int, int> pos;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> A[i];
  for (int i = 0; i < n; ++i) cin >> B[i];
  int brk = 0, xr = 0;
  for (int i = 0; i < n; ++i) {
    xr ^= A[i];
    brk += A[i] != B[i];
    if (pos.count(B[i]) == 0)
      pos[B[i]] = i;
    else
      pos[B[i]] = -1;
  }
  if (brk == 0) return cout << "0\n", 0;
  int x = xr, t = 0;
  while (pos.count(x) && pos[x] != -1 && pos[x] != -2) {
    ++t;
    int y = A[pos[x]];
    pos[x] = -2;
    x = y;
  }
  bool inc = !(pos.count(x) && pos[x] == -1 || t == brk);
  A[n] = xr;
  sort(A, A + n + 1);
  sort(B, B + n);
  int ptr = 0;
  for (int i = 0; i < n; ++i) {
    while (ptr <= n && A[ptr] != B[i]) ++ptr;
    if (ptr > n) {
      cout << "-1\n";
      return 0;
    }
    ++ptr;
  }
  if (inc) ++brk;
  cout << brk << '\n';
}
