#include <bits/stdc++.h>
using namespace std;
int a[100005];
int b[100005];
int c[100005];
int d[100005];
int p[100005];
int rank1[100005];
int N;
int numSets;
void init() {
  numSets = N;
  for (int i = 0; i <= N; i++) {
    p[i] = i;
    rank1[i] = 0;
  }
}
int findSet(int x) { return (p[x] == x) ? x : p[x] = findSet(p[x]); }
bool isSameSet(int u, int v) { return findSet(u) == findSet(v); }
void unionSet(int i, int j) {
  if (!isSameSet(i, j)) {
    int x = findSet(i);
    int y = findSet(j);
    if (rank1[x] > rank1[y]) {
      p[y] = x;
      numSets--;
    } else {
      p[x] = y;
      numSets--;
      if (rank1[x] == rank1[y]) {
        rank1[y]++;
      }
    }
  }
}
int main() {
  scanf("%d", &N);
  int xorsum = 0;
  for (int i = 0; i < N; i++) {
    scanf("%d", &a[i]);
    c[i] = a[i];
    xorsum ^= a[i];
  }
  a[N] = xorsum;
  c[N] = xorsum;
  xorsum = 0;
  for (int i = 0; i < N; i++) {
    scanf("%d", &b[i]);
    d[i] = b[i];
    xorsum ^= b[i];
  }
  b[N] = xorsum;
  d[N] = xorsum;
  sort(c, c + (N + 1));
  sort(d, d + (N + 1));
  for (int i = 0; i < N; i++) {
    if (c[i] != d[i]) {
      printf("-1");
      return 0;
    }
  }
  bool ansIs0 = true;
  for (int i = 0; i < N; i++) {
    if (a[i] != b[i]) {
      ansIs0 = false;
      break;
    }
  }
  if (ansIs0) {
    printf("0");
    return 0;
  }
  vector<int> xingchen;
  set<int> s;
  for (int i = 0; i <= N; i++) {
    xingchen.push_back(a[i]);
    s.insert(a[i]);
  }
  assert(s.size() == N + 1);
  sort(xingchen.begin(), xingchen.end());
  xingchen.erase(unique(xingchen.begin(), xingchen.end()), xingchen.end());
  int ans = 0;
  init();
  for (int i = 0; i <= N; i++) {
    int indxA =
        lower_bound(xingchen.begin(), xingchen.end(), a[i]) - xingchen.begin();
    int indxB =
        lower_bound(xingchen.begin(), xingchen.end(), b[i]) - xingchen.begin();
    if (indxA != indxB) {
      ans++;
      unionSet(indxA, indxB);
    } else {
      numSets--;
    }
  }
  if (a[N] == b[N]) {
    ans++;
  }
  printf("%d", ans + numSets - 1);
  return 0;
}
