#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int SIZE = 1e6 + 10;
int a[SIZE], b[SIZE], d[2][SIZE], c[SIZE], cn;
set<pair<int, int> > need[SIZE];
struct Union_Find {
  int d[SIZE], num[SIZE];
  void init(int n) {
    for (int i = 0; i < (n); ++i) d[i] = i, num[i] = 1;
  }
  int find(int x) { return (x != d[x]) ? (d[x] = find(d[x])) : x; }
  bool uu(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return 0;
    if (num[x] > num[y]) swap(x, y);
    num[y] += num[x];
    d[x] = y;
    return 1;
  }
} U;
int main() {
  int(N);
  scanf("%d", &N);
  for (int i = 0; i < (N); ++i) {
    scanf("%d", &(a[i]));
    a[N] ^= a[i];
  }
  for (int i = 0; i < (N); ++i) {
    scanf("%d", &(b[i]));
    b[N] ^= b[i];
  }
  for (int i = 0; i < (N + 1); ++i) {
    d[0][i] = a[i];
    d[1][i] = b[i];
  }
  for (int i = 0; i < (2); ++i) sort(d[i], d[i] + N + 1);
  for (int i = 0; i < (N + 1); ++i) {
    if (d[0][i] != d[1][i]) return 0 * puts("-1");
    c[i] = d[0][i];
  }
  cn = unique(c, c + N + 1) - c;
  for (int i = 0; i < (N + 1); ++i) {
    a[i] = lower_bound(c, c + cn, a[i]) - c;
    b[i] = lower_bound(c, c + cn, b[i]) - c;
  }
  U.init(N + 1);
  int v = 0;
  for (int i = 0; i < (N + 1); ++i) {
    if (a[i] != b[i]) {
      v++;
      U.uu(a[i], b[i]);
    }
  }
  if (!v) return 0 * puts("0");
  if (a[N] == b[N]) v++;
  v -= 2;
  for (int i = 0; i < (N + 1); ++i) {
    if (U.find(i) == i && U.num[i] > 1) v++;
  }
  printf("%d\n", v);
  return 0;
}
