#include <bits/stdc++.h>
using namespace std;
struct UF {
  vector<int> par;
  vector<int> sz;
  vector<int> rank;
  UF(int n) : par(n), sz(n), rank(n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      sz[i] = 1;
      rank[i] = 0;
    }
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (rank[x] < rank[y]) {
      par[x] = y;
      sz[y] += sz[x];
    } else {
      par[y] = x;
      sz[x] += sz[y];
      if (rank[x] == rank[y]) rank[x]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int n) { return sz[find(n)]; }
};
bool check(vector<int> a, vector<int> b) {
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  for (long long(i) = (0); (i) < (a.size()); ++i) {
    if (a[i] != b[i]) return false;
  }
  return true;
}
void add_xor(vector<int> &v) {
  int tmp = 0;
  for (long long(i) = (0); (i) < (v.size()); ++i) tmp ^= v[i];
  v.push_back(tmp);
}
void compression(vector<int> a, vector<int> b) {
  vector<int> v;
  for (long long(i) = (0); (i) < (a.size()); ++i) v.push_back(a[i]);
  for (long long(i) = (0); (i) < (b.size()); ++i) v.push_back(b[i]);
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  for (long long(i) = (0); (i) < (a.size()); ++i)
    a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
  for (long long(i) = (0); (i) < (b.size()); ++i)
    b[i] = lower_bound(v.begin(), v.end(), b[i]) - v.begin();
}
int seica(vector<int> a, vector<int> b) {
  int ret = 0;
  add_xor(a);
  add_xor(b);
  if (!check(a, b)) return -1;
  compression(a, b);
  UF uf(a.size());
  for (long long(i) = (0); (i) < ((int)a.size() - 1); ++i) {
    if (a[i] != b[i]) ret++;
    uf.unite(a[i], b[i]);
  }
  vector<bool> used(a.size(), false);
  for (long long(i) = (0); (i) < (a.size()); ++i) {
    int now = uf.find(a[i]);
    if (now < 0 || now >= used.size()) {
      cout << -1 << endl;
      exit(0);
    }
    if (i == (int)a.size() - 1 && uf.size(now) > 1) ret--;
    if (used[now] || uf.size(now) == 1) continue;
    used[now] = true;
    ret++;
  }
  return ret;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n);
  for (long long(i) = (0); (i) < (n); ++i) cin >> a[i];
  vector<int> b(n);
  for (long long(i) = (0); (i) < (n); ++i) cin >> b[i];
  cout << seica(a, b) << endl;
  return 0;
}
