#include <bits/stdc++.h>
using namespace std;
int a[200000], b[200000];
vector<int> E[200000];
bool used[200000];
int deg[200000];
void dfs(int v) {
  used[v] = true;
  for (int u : E[v]) {
    if (!used[u]) dfs(u);
  }
}
int main() {
  int n;
  cin >> n;
  map<int, int> mp;
  vector<int> vs;
  int x = 0;
  for (int i = 0; i < (n); i++) {
    scanf("%d", &a[i]);
    mp[a[i]]++;
    x ^= a[i];
    vs.push_back(a[i]);
  }
  a[n] = x;
  mp[x]++;
  vs.push_back(x);
  x = 0;
  for (int i = 0; i < (n); i++) {
    scanf("%d", &b[i]);
    mp[b[i]]--;
    x ^= b[i];
  }
  b[n] = x;
  mp[x]--;
  for (auto p : mp) {
    if (p.second) {
      puts("-1");
      return 0;
    }
  }
  map<int, vector<int>> want;
  for (int i = 0; i < (n); i++) {
    if (a[i] != b[i]) {
      want[b[i]].push_back(i);
    }
  }
  if (want.empty()) {
    puts("0");
    return 0;
  }
  sort(vs.begin(), vs.end());
  vs.erase(unique(vs.begin(), vs.end()), vs.end());
  int cnt = 0;
  for (int i = 0; i < (n + 1); i++) {
    a[i] = lower_bound(vs.begin(), vs.end(), a[i]) - vs.begin();
    b[i] = lower_bound(vs.begin(), vs.end(), b[i]) - vs.begin();
    if (i == n || a[i] == b[i]) continue;
    cnt++;
    E[b[i]].push_back(a[i]);
    deg[a[i]]++;
    deg[b[i]]++;
  }
  int K = 0;
  for (int i = 0; i < (vs.size()); i++) {
    if (deg[i] && !used[i]) {
      K++;
      dfs(i);
    }
  }
  if (a[n] == b[n]) {
    cout << cnt + K << endl;
  } else {
    abort();
  }
}
