#include <bits/stdc++.h>
using namespace std;
long long int N, a[114514], b[114514], r;
pair<long long, long long> c[114514], d[114514], e, f;
int main() {
  scanf("%lld", &N);
  for (int i = 0; i < N; ++i) {
    scanf("%lld", a + i);
    a[N] ^= a[i];
    c[i] = make_pair(a[i], i);
  }
  c[N] = make_pair(a[N], N);
  sort(c, c + N + 1);
  for (int i = 0; i < N; ++i) {
    scanf("%lld", b + i);
    b[N] ^= b[i];
    d[i] = make_pair(b[i], i);
  }
  d[N] = make_pair(b[N], N);
  sort(d, d + N + 1);
  for (int i = 0; i <= N; ++i) {
    if (c[i].first != d[i].first) return puts("-1") & 0;
  }
  for (int i = 0; i < N; ++i) {
    if (a[i] != b[i]) {
      if (b[i] == a[N]) {
        ++r;
        swap(a[i], a[N]);
        e = make_pair(a[N], N);
        f = make_pair(a[i], i);
        swap(lower_bound(c, c + N + 1, e)->second,
             lower_bound(c, c + N + 1, f)->second);
      } else {
        r += 2;
        e = make_pair(b[i], 0);
        swap(a[lower_bound(c, c + N + 1, e)->second], a[N]);
        e = make_pair(b[i], 0);
        f = make_pair(a[N], N);
        swap(lower_bound(c, c + N + 1, e)->second,
             lower_bound(c, c + N + 1, f)->second);
        swap(a[i], a[N]);
        e = make_pair(a[i], i);
        f = make_pair(a[N], N);
        swap(lower_bound(c, c + N + 1, f)->second,
             lower_bound(c, c + N + 1, e)->second);
      }
    }
  }
  cout << r << endl;
  return 0;
}
