#include <bits/stdc++.h>
using namespace std;
int in() {
  int a;
  scanf("%d ", &a);
  return a;
}
int main() {
  int N = in();
  vector<int> a(N + 1), b(N + 1);
  int hoge = 0, fuga = 0;
  for (int i = 0; i < (N); i++) hoge ^= a[i] = in();
  for (int i = 0; i < (N); i++) fuga ^= b[i] = in();
  a[N] = hoge, b[N] = fuga;
  vector<int> c = a, d = b;
  sort(c.begin(), c.end());
  sort(d.begin(), d.end());
  for (int i = 0; i < (N + 1); i++) {
    if (c[i] != d[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  map<int, vector<int> *> data;
  int siz = 0;
  set<int> ind;
  for (int i = 0; i < (N); i++) {
    if (a[i] == b[i]) continue;
    ind.insert(i);
    if (data.find(b[i]) == data.end()) {
      data[b[i]] = new vector<int>(1, i);
    } else {
      data[b[i]]->push_back(i);
    }
    siz++;
  }
  int ans = 0;
  int t = a[N];
  for (; siz > 0;) {
    ans++;
    vector<int> *v = data[t];
    if (v && v->size() > 0) {
      int j = v->at(v->size() - 1);
      v->pop_back();
      ind.erase(j);
      int temp = a[j];
      a[j] = t;
      t = temp;
      siz--;
    } else {
      int j = *(ind.begin());
      if (a[j] == t) j = *(ind.rbegin());
      int temp = a[j];
      a[j] = t;
      t = temp;
    }
  }
  cout << ans << endl;
  return 0;
}
