#include <bits/stdc++.h>
using namespace std;
void RI() {}
template <typename... T>
void RI(int& head, T&... tail) {
  scanf("%d", &head);
  RI(tail...);
}
void PI() { putchar('\n'); }
template <typename... T>
void PI(const int head, T... tail) {
  printf("%d ", head);
  PI(tail...);
}
const int MOD = 1000000007;
int a[100005];
int b[100005];
unordered_map<int, unordered_set<int> > posb;
unordered_multiset<int> notok;
int main() {
  int N, cur = 0;
  RI(N);
  for (int i = 0; i < (int)(N); i++) {
    RI(a[i]);
    cur ^= a[i];
  }
  for (int i = 0; i < (int)(N); i++) {
    RI(b[i]);
    if (b[i] != a[i]) {
      notok.insert(b[i]);
      posb[b[i]].insert(i);
    }
  }
  int bad = -1, cnt_bad = 0;
  if (notok.find(cur) == notok.end()) {
    cnt_bad += 1;
  }
  for (int i = 0; i < (int)(N); i++) {
    if (b[i] != a[i]) {
      if (notok.find(a[i]) == notok.end()) {
        bad = i;
        cnt_bad += 1;
      }
    }
  }
  if (cnt_bad > 1) {
    printf("-1\n");
    return 0;
  }
  int cnt = 0;
  auto ii = notok.find(cur);
  while (!notok.empty()) {
    cnt++;
    if (ii != notok.end()) {
      int pos = *(posb[cur].begin());
      if (pos == bad and posb[cur].size() > 1) {
        pos = *(++posb[cur].begin());
      }
      posb[cur].erase(pos);
      notok.erase(ii);
      swap(cur, a[pos]);
      if (pos == bad) {
        bad = -1;
      }
    } else {
      int value = *(notok.begin());
      int pos = *(posb[value].begin());
      swap(cur, a[pos]);
      if (bad == -1) {
        bad = pos;
      }
    }
    ii = notok.find(cur);
  }
  printf("%d\n", cnt);
  return 0;
}
