#include <bits/stdc++.h>
using namespace std;
int ans, N, a[100009], b[100009], p[100009];
bool ap[100009];
pair<int, int> c[100009], d[100009];
void checkPossibility() {
  for (int i = 1; i <= N + 1; i++) c[i] = {a[i], i}, d[i] = {b[i], i};
  sort(c + 1, c + N + 2), sort(d + 1, d + N + 2);
  for (int i = 1; i <= N + 1; i++)
    if (c[i].first != d[i].first) {
      printf("-1\n");
      exit(0);
    }
}
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) scanf("%d", &a[i]), a[N + 1] ^= a[i];
  for (int i = 1; i <= N; i++) scanf("%d", &b[i]), b[N + 1] ^= b[i];
  checkPossibility();
  ans = 0;
  bool ok = 0;
  for (int i = 1; i <= N; i++) {
    ans += (a[i] != b[i]);
    if (a[i] != b[i] && a[i] == b[N + 1] && i <= N) ok = 1;
  }
  if (a[N + 1] == b[N + 1] && ans != 0)
    ans++;
  else if (ok == 0 && a[N + 1] != b[N + 1])
    ans++;
  printf("%d\n", ans);
  return 0;
}
