#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long double pi = acos(-1);
const int MOD = 1e9 + 7;
const long long int LINF = 0x3f3f3f3f3f3f3f3f;
int n;
int a[100005];
int b[100005];
map<int, int> ma;
set<int> sa;
map<int, int> mb;
int main() {
  scanf("%d", &n);
  int x = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    ma[a[i]]++;
    sa.insert(a[i]);
    x = x ^ a[i];
  }
  a[n] = x;
  sa.insert(x);
  ma[x]++;
  for (int i = 0; i < n; i++) {
    scanf("%d", &b[i]);
    mb[b[i]]++;
  }
  for (auto p : mb) {
    ma[p.first] -= p.second;
  }
  int dif = 0;
  for (auto p : ma) {
    if (p.second > 1 or p.second < 0) {
      dif = 1000;
    } else if (p.second != 0) {
      dif++;
      if (!sa.count(x)) dif = 1000;
    }
  }
  if (dif > 1) {
    printf("%d\n", -1);
    return 0;
  }
  int resp = 0;
  set<int> fora;
  map<int, set<int> > foraporval;
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      fora.insert(i);
      foraporval[b[i]].insert(i);
    }
  }
  int poeagora = a[n];
  while (!fora.empty()) {
    if (!foraporval[poeagora].empty()) {
      int idx = *foraporval[poeagora].begin();
      swap(a[idx], a[n]);
      foraporval[poeagora].erase(idx);
      fora.erase(idx);
    } else {
      int idx = *fora.begin();
      swap(a[idx], a[n]);
    }
    poeagora = a[n];
    resp++;
  }
  printf("%d\n", resp);
  return 0;
}
