#include <bits/stdc++.h>
const int N = 1e5 + 10;
using namespace std;
int n, m, a[N], b[N], A[N], B[N];
int x, y, ans, fa[N], tot;
int get(int x) { return !fa[x] ? x : fa[x] = get(fa[x]); }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), x = x ^ a[i];
  a[n + 1] = x;
  x = 0;
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]), x = x ^ b[i];
  b[n + 1] = x;
  x = 0;
  memcpy(A, a, sizeof A), memcpy(B, b, sizeof B);
  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;
    }
  for (int i = 1; i <= n; i++)
    if (a[i] ^ b[i]) {
      x = get(a[i]), y = get(b[i]), ans += (i <= n);
      if (x ^ y) fa[x] = y;
    }
  for (int i = 1; i <= n + 1; i++)
    if (a[i] ^ b[i] && (get(a[i]) == a[i])) ans++, tot++;
  if (ans == 0) {
    puts("0");
    return 0;
  }
  ans++;
  for (int i = 1; i <= n; i++)
    if (b[i] == a[n + 1] && a[i] ^ b[i]) {
      ans--;
      break;
    }
  printf("%d\n", ans - (tot > 0));
}
