#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int n;
int a[MAXN], b[MAXN];
set<pair<int, int> > v;
int main() {
  scanf("%d", &n);
  map<int, int> F, G;
  int x = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    x ^= a[i];
    F[a[i]]++;
  }
  F[x]++;
  x = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &b[i]);
    x ^= b[i];
    G[b[i]]++;
  }
  G[x]++;
  if (F != G) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i <= n; ++i) {
    if (a[i] != b[i]) v.insert(pair<int, int>(b[i], i));
  }
  int ans = 0;
  int cur = 0;
  for (int i = 1; i <= n; ++i) cur ^= a[i];
  while (!v.empty()) {
    ans++;
    auto it = v.lower_bound(pair<int, int>(cur, 0));
    if (it == v.end()) it = v.begin();
    int tmp = a[it->second];
    a[it->second] = cur;
    cur = tmp;
    if (a[it->second] == b[it->second]) v.erase(it);
  }
  cout << ans << endl;
  return 0;
}
