#include <bits/stdc++.h>
using namespace std;
template <class S, class T>
ostream& operator<<(ostream& o, const pair<S, T>& p) {
  return o << "(" << p.first << "," << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& vc) {
  o << "sz = " << vc.size() << endl << "[";
  for (const T& v : vc) o << v << ",";
  o << "]";
  return o;
}
int N;
const int MN = 100010;
int a[MN + 1];
int b[MN + 1];
struct UnionFind {
  int par[MN], rank[MN];
  void init(int N) {
    for (int i = 0; i < (int)(N); i++) par[i] = i, rank[i] = 0;
  }
  int find(int x) {
    if (par[x] == x) return x;
    return par[x] = find(par[x]);
  }
  bool same(int x, int y) { return find(x) == find(y); }
  void unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y) return;
    if (rank[x] < rank[y])
      par[x] = y;
    else {
      par[y] = x;
      if (rank[x] == rank[y]) rank[x]++;
    }
  }
} UF;
int main() {
  cin >> N;
  int s = 0;
  for (int i = 0; i < (int)(N); i++) cin >> a[i], s ^= a[i];
  a[N] = s;
  s = 0;
  for (int i = 0; i < (int)(N); i++) cin >> b[i], s ^= b[i];
  b[N] = s;
  map<int, int> cnt;
  map<int, int> cand;
  for (int i = 0; i < (int)(N + 1); i++)
    if (a[i] != b[i]) {
      cnt[a[i]]++;
      cnt[b[i]]--;
      cand[a[i]] = i;
      cand[b[i]] = i;
    }
  for (auto it : cnt) {
    if (it.second != 0) {
      puts("-1");
      return 0;
    }
  }
  UF.init(N + 1);
  int dif = 0;
  for (int i = 0; i < (int)(N + 1); i++) {
    if (i != N && a[i] != b[i]) {
      dif++;
    }
    UF.unite(i, cand[a[i]]);
    UF.unite(i, cand[b[i]]);
  }
  int r = 0;
  for (int i = 0; i < (int)(N + 1); i++)
    if (UF.find(i) == i) r++;
  cout << dif + r - 1 << endl;
}
