#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
  vector<int> par;
  vector<int> rank;
  vector<long long> Size;
  bool checked[300050];
  UnionFind(int n = 1) { init(n); }
  void init(int n = 1) {
    par.resize(n + 1);
    rank.resize(n + 1);
    Size.resize(n + 1);
    for (int i = 0; i <= n; ++i)
      par[i] = i, rank[i] = 0, Size[i] = 0, checked[i] = false;
  }
  int root(int x) {
    if (par[x] == x) {
      return x;
    } else {
      int r = root(par[x]);
      return par[x] = r;
    }
  }
  bool issame(int x, int y) { return root(x) == root(y); }
  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return false;
    if (rank[x] < rank[y]) swap(x, y);
    if (rank[x] == rank[y]) ++rank[x];
    par[y] = x;
    Size[x] += Size[y];
    return true;
  }
  long long size(int x) { return Size[root(x)]; }
  long long add(int x) { Size[root(x)]++; }
};
int main() {
  map<int, int> counter;
  int N;
  cin >> N;
  int a[100050];
  int b[100050];
  UnionFind uni(300000);
  int outer = 0;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
    counter[a[i]]++;
    outer ^= a[i];
  }
  a[N + 1] = outer;
  counter[outer]++;
  int outer_b = 0;
  for (int i = 1; i <= N; i++) {
    cin >> b[i];
    if (counter[b[i]] == 0) {
      cout << -1 << endl;
      return 0;
    }
    if (a[i] != b[i]) {
      uni.merge(a[i], b[i]);
      uni.add(b[i]);
    }
    counter[b[i]]--;
    outer_b ^= b[i];
  }
  uni.merge(outer, outer_b);
  uni.add(outer_b);
  for (int i = 0; i <= 5; i++) {
  }
  int ans = 0;
  for (int i = 0; i <= 250000; i++) {
    if (uni.checked[uni.root(i)]) continue;
    uni.checked[uni.root(i)] = true;
    if (uni.size(i) <= 1) continue;
    if (uni.issame(i, outer))
      ans += uni.size(i) - 1;
    else
      ans += uni.size(i) + 1;
  }
  cout << ans << endl;
  return 0;
}
