#include <bits/stdc++.h>
using namespace std;
int lsh[2100000], cnt, num[2100000];
int n, a[50005], b[50055], fa[50005], a1[52005], b1[50005];
int yh;
int ans;
int get(int x) {
  if (fa[x] != x) fa[x] = get(fa[x]);
  return fa[x];
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a1[i] = a[i];
    yh ^= a[i];
  }
  a[n + 1] = yh;
  a1[n + 1] = yh;
  yh = 0;
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    b1[i] = b[i];
    yh ^= b[i];
  }
  b[n + 1] = yh;
  b1[n + 1] = yh;
  sort(a1 + 1, a1 + n + 2), sort(b1 + 1, b1 + n + 2);
  for (int i = 1; i <= n + 1; i++)
    if (a1[i] != b1[i]) {
      cout << "-1";
      return 0;
    }
  n++;
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= n; i++) lsh[i] = a[i];
  sort(lsh + 1, lsh + 1 + n);
  cnt = unique(lsh + 1, lsh + n + 1) - lsh - 1;
  for (int i = 1; i <= n; i++)
    a[i] = lower_bound(lsh + 1, lsh + cnt + 1, a[i]) - lsh;
  memset(lsh, 0, sizeof(lsh));
  for (int i = 1; i <= n; i++) lsh[i] = b[i];
  sort(lsh + 1, lsh + n + 1);
  cnt = unique(lsh + 1, lsh + n + 1) - lsh - 1;
  for (int i = 1; i <= n; i++)
    b[i] = lower_bound(lsh + 1, lsh + cnt + 1, b[i]) - lsh;
  for (int i = 1; i <= n; i++)
    for (int i = 1; i <= n; i++) {
      if (a[i] != b[i]) {
        int rx = get(a[i]), ry = get(b[i]);
        if (rx != ry) {
          fa[ry] = rx;
          ans++;
        }
      }
    }
  for (int i = 1; i <= cnt; i++) {
    if (fa[i] == i) ans++;
  }
  if (fa[n] != a[n]) ans--;
  cout << ans - 1;
  return 0;
}
