#include <bits/stdc++.h>
using namespace std;
const int mxn = 1 << 17;
int n, a[mxn], b[mxn], f[mxn];
pair<int, int> a1[mxn], b1[mxn];
char bo[mxn], vis[mxn];
int get(int x) { return f[x] ? f[x] = get(f[x]) : x; }
inline void link(int a, int b) {
  if (get(a + 1) != get(b + 1)) f[get(a + 1)] = get(b + 1);
}
int main() {
  int i, j, k, l;
  scanf("%d", &n);
  for (i = 0; i < n; ++i) scanf("%d", a + i), a[n] ^= a[i];
  for (i = 0; i < n; ++i) scanf("%d", b + i), b[n] ^= b[i];
  k = n + 1;
  for (i = j = 0; i < n + 1; ++i) {
    if (i < n && a[i] == b[i])
      bo[i] = 1, --k;
    else
      a1[j] = make_pair(a[i], i), b1[j++] = make_pair(b[i], i);
  }
  sort(a1, a1 + j), sort(b1, b1 + j);
  for (i = 0; i < j; ++i)
    if (a1[i].first != b1[i].first) goto NO;
  for (i = 0; i < n + 1; ++i)
    if (!bo[i]) link(i, lower_bound(b1, b1 + j, make_pair(a[i], 0))->second);
  for (i = 0; i < j - 1; ++i)
    if (b1[i].first == b1[i + 1].first) link(i, i + 1);
  for (i = 0; i < n + 1; ++i)
    if (!bo[i] && !f[i + 1]) ++k;
  k -= 2;
  printf("%d\n", k);
  return 0;
NO:
  return puts("-1"), 0;
}
