#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  register long long x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + ch - '0';
    ch = getchar();
  }
  return (f == 1) ? x : -x;
}
int n, a[101010], b[101010], A[101010], B[101010];
map<int, int> mp;
int F[101010], cnt[101010];
int find(int x) { return F[x] == x ? x : F[x] = find(F[x]); }
void merge(int x, int y) {
  int xx = find(x), yy = find(y);
  if (xx != yy) F[xx] = yy;
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) a[i] = read(), a[n + 1] ^= a[i];
  for (int i = 1; i <= n; i++) b[i] = read(), b[n + 1] ^= b[i];
  for (int i = 1; i <= n + 1; i++) {
    A[i] = a[i];
    B[i] = b[i];
  }
  sort(A + 1, A + n + 2), sort(B + 1, B + n + 2);
  for (int i = 1; i <= n + 1; i++) {
    if (A[i] != B[i]) {
      puts("-1");
      return 0;
    }
  }
  int tot = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] != b[i]) a[++tot] = a[i], b[tot] = b[i];
  }
  a[tot + 1] = a[tot + 1], b[tot + 1] = b[tot + 1];
  n = tot;
  int m = 0;
  for (int i = 1; i <= n + 1; i++) {
    a[i] = mp[a[i]] ? mp[a[i]] : mp[a[i]] = ++m;
  }
  for (int i = 1; i <= n + 1; i++) b[i] = mp[b[i]];
  for (int i = 1; i <= m; i++) F[i] = i;
  for (int i = 1; i <= n + 1; i++) merge(b[i], a[i]);
  for (int i = 1; i <= m; i++) cnt[find(i)]++;
  int ans = n - 1;
  for (int i = 1; i <= m; i++) {
    if (cnt[i]) ans++;
  }
  printf("%d", ans);
  return 0;
}
