#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 5;
int a[MAX];
int b[MAX];
int c[MAX];
int d[MAX];
map<int, int> p;
set<int> help;
int find(int x) {
  if (p.find(x) == p.end()) {
    p[x] = x;
    return x;
  } else if (p[x] == x) {
    return x;
  }
  return find(p[x]);
}
int main() {
  int n;
  scanf("%d", &n);
  int x = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    x ^= a[i];
  }
  a[n + 1] = x;
  x = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    x ^= b[i];
  }
  b[n + 1] = x;
  n++;
  for (int i = 1; i <= n; i++) {
    c[i] = a[i];
    d[i] = b[i];
  }
  sort(c + 1, c + n + 1);
  sort(d + 1, d + n + 1);
  int flg = 0;
  for (int i = 1; i <= n; i++) {
    if (c[i] != d[i]) {
      flg = 1;
      break;
    }
  }
  if (flg == 1) {
    printf("-1\n");
    return 0;
  }
  int ans = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] != b[i]) {
      ans++;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] != b[i] || (i == n)) {
      int aa = find(a[i]);
      int bb = find(b[i]);
      if (aa != bb) {
        p[aa] = bb;
      }
    }
  }
  for (map<int, int>::iterator it = p.begin(); it != p.end(); it++) {
    help.insert(it->second);
  }
  ans += help.size() - 1;
  printf("%d\n", ans);
}
