#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 main() {
  int i, j, k = 0;
  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];
  for (i = 0; i < n; ++i) {
    if (a[i] == b[i])
      bo[i] = 1;
    else if (a[i] == b[n])
      swap(a[i], a[n]), --k;
    a1[i] = make_pair(a[i], i), b1[i] = make_pair(b[i], i);
  }
  sort(a1, a1 + n), sort(b1, b1 + n);
  for (i = j = 0; i < n && j < n; ++i, ++j) {
    for (; i < n && bo[a1[i].second];) ++i;
    for (; j < n && bo[b1[j].second];) ++j;
    if (i < n && j < n)
      if (a1[i].first != b1[j].first)
        goto NO;
      else
        f[a1[i].second] = b1[j].second;
  }
  if (i != j) goto NO;
  k += n;
  for (i = 0; i < n; ++i)
    if (!vis[i]) {
      if (bo[i]) {
        --k;
        continue;
      }
      for (j = i; f[j] != i; j = f[j]) vis[j] = 1;
      vis[j] = 1, ++k;
    }
  printf("%d\n", k);
  return 0;
NO:
  return puts("-1"), 0;
}
