#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int a[100005];
int b[100005];
int sa[100005];
int sb[100005];
map<int, queue<int> > M;
int main() {
  int n;
  scanf("%d", &n);
  int xa = 0, xb = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
    sa[i] = a[i];
    xa ^= a[i];
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", b + i);
    sb[i] = b[i];
    xb ^= b[i];
  }
  a[n] = sa[n] = xa;
  b[n] = sb[n] = xb;
  sort(sa, sa + n + 1);
  sort(sb, sb + n + 1);
  for (int i = 0; i < n + 1; i++) {
    if (sa[i] != sb[i]) {
      printf("-1\n");
      return 0;
    }
    if (a[i] != b[i]) {
      M[b[i]].push(i);
    }
  }
  int pos = 0;
  int cnt = 0;
  while (1) {
    while (!M[a[n]].empty()) {
      int first = a[n];
      swap(a[n], a[M[first].front()]);
      M[first].pop();
      cnt++;
    }
    while (pos <= n and a[pos] == b[pos]) pos++;
    if (pos == n + 1) break;
    swap(a[pos], a[n]);
    cnt++;
  }
  printf("%d\n", cnt);
  return 0;
}
