#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n;
int a[N], b[N];
map<int, vector<int> > indices;
int perm[N];
bool seen[N];
int main() {
  cin >> n;
  a[n] = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[n] ^= a[i];
  }
  b[n] = 0;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    b[n] ^= b[i];
  }
  for (int i = 0; i <= n; i++) {
    if (a[i] != b[i]) {
      if (indices.count(a[i]) == 0) indices[a[i]] = vector<int>();
      indices[a[i]].push_back(i);
    }
  }
  for (int i = 0; i <= n; i++) {
    if (a[i] == b[i]) {
      perm[i] = i;
    } else if (indices[b[i]].size() == 0) {
      cout << "-1\n";
      return 0;
    } else {
      perm[i] = indices[b[i]].back();
      indices[b[i]].pop_back();
    }
  }
  int ret = n - 1;
  for (int i = 0; i <= n; i++) {
    if (!seen[i]) {
      ret++;
      int cur = i;
      while (!seen[cur]) {
        seen[cur] = true;
        cur = perm[cur];
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (perm[i] == i) ret -= 2;
  }
  if (a[n] == b[n]) {
    for (int i = 0; i < n; i++) {
      if (a[i] == a[n]) ret--;
    }
  }
  cout << ret << "\n";
}
