#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000;
int N;
long long int a[MAX_N + 1], sab[2][MAX_N + 1];
bool visited[MAX_N + 1];
multimap<long long int, int> mm;
int main() {
  long long int x;
  auto itr = mm.end();
  bool x_used;
  int ans;
  cin >> N;
  for (int j = 0; j < 2; j++) {
    x = 0;
    for (int i = 0; i <= N; i++) {
      if (i == N) {
        sab[j][N] = x;
      } else {
        cin >> sab[j][i];
        x = x ^ sab[j][i];
      }
      if (j == 1) {
        if (sab[0][i] == sab[1][i]) {
          visited[i] = true;
        } else {
          visited[i] = false;
          mm.insert(pair<long long int, int>(sab[1][i], i));
        }
      }
    }
  }
  x_used = (sab[0][N] != sab[1][N]);
  copy(sab[0], sab[0] + N + 1, a);
  sort(sab[0], sab[0] + N + 1);
  sort(sab[1], sab[1] + N + 1);
  for (int i = 0; i <= N; i++) {
    if (sab[0][i] != sab[1][i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  ans = (int)mm.size();
  ans += x_used ? -2 : 0;
  for (int i = 0; i <= N; i++) {
    if (visited[i]) {
      continue;
    }
    ans++;
    int j = i;
    while (!visited[j]) {
      visited[j] = true;
      itr = mm.find(a[j]);
      j = itr->second;
      mm.erase(itr);
      if (!x_used && a[j] == x) {
        x_used = true;
        ans--;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
