#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
unordered_map<int, int> id;
vector<int> v;
int a[N], b[N], p[N], rk[N], xa[N], xb[N];
int n, m, num;
int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); }
void unionset(int x, int y) {
  x = find(x), y = find(y);
  if (x == y) return;
  if (rk[x] < rk[y]) swap(x, y);
  p[y] = x;
  if (rk[x] == rk[y]) rk[x]++;
  num--;
}
int main() {
  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; i++) {
    xa[i] = a[i], xb[i] = b[i];
  }
  sort(xa, xa + n + 1);
  sort(xb, xb + n + 1);
  for (int i = 0; i <= n; i++) {
    if (xa[i] != xb[i]) {
      printf("-1");
      return 0;
    }
  }
  for (int i = 0; i <= n; i++) {
    if (a[i] != b[i] || i == n) {
      v.push_back(a[i]);
    }
  }
  m = (int)v.size();
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  for (auto x : v) {
    id[x] = num++;
  }
  num--;
  iota(p, p + N, 0);
  for (int i = 0; i <= n; i++) {
    if (a[i] != b[i] || i == n) {
      unionset(id[a[i]], id[b[i]]);
    }
  }
  int ans = N + num - 2;
  printf("%d", ans);
}
