#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];
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;
  }
  for (int i = 0; i < (N); ++i) {
    if (a[i] != b[i]) {
      need[b[i]].insert(make_pair(a[i], i));
    }
  }
  int it = 0;
  int v = 0;
  while (it < N) {
    int x = a[N];
    if (((int)(need[x]).size())) {
      pair<int, int> ch = *need[x].begin();
      if (ch.first == b[N]) ch = *need[x].rbegin();
      need[x].erase(ch);
      v++;
      swap(a[ch.second], a[N]);
    } else {
      if (a[it] != b[it]) {
        int x = b[it];
        need[x].erase(make_pair(a[it], it));
        swap(a[it], a[N]);
        need[x].insert(make_pair(a[it], it));
        v++;
      } else
        it++;
    }
  }
  printf("%d\n", v);
  return 0;
}
