#include <bits/stdc++.h>
using namespace std;
vector<int> get(vector<int> a, vector<int> b) {
  multiset<int> kek;
  for (int i = 0; i < a.size(); i++) kek.insert(a[i]);
  for (int i = 0; i < b.size(); i++) {
    if (!kek.count(b[i])) continue;
    kek.erase(kek.find(b[i]));
  }
  vector<int> res;
  if (!kek.size()) return {};
  for (auto it = kek.begin(); it != kek.end(); it++) {
    res.push_back(*it);
  }
  return res;
}
struct Snm {
  int f;
  int r;
};
vector<Snm> snm;
int g(int v) {
  if (snm[v].f != v) snm[v].f = g(snm[v].f);
  return snm[v].f;
}
void un(int u, int v) {
  if (u == v) return;
  if (snm[u].r > snm[v].r) swap(u, v);
  snm[u].f = v;
  if (snm[u].r == snm[v].r) snm[v].r++;
}
void solve(vector<int> a, vector<int> b, int add) {
  vector<int> c, d;
  for (int i = 0; i < a.size(); i++) {
    if (a[i] == b[i]) continue;
    c.push_back(a[i]);
    d.push_back(b[i]);
  }
  int n = c.size();
  if (n == 0) {
    cout << add;
    exit(0);
  }
  for (int i = 0; i < n; i++) {
    snm.push_back({i, 1});
  }
  map<int, int> last;
  for (int i = 0; i < n; i++) {
    if (last.count(c[i])) un(g(last[c[i]]), g(i));
    if (last.count(d[i])) un(g(last[d[i]]), g(i));
    last[c[i]] = i;
    last[d[i]] = i;
  }
  set<int> tet;
  for (int i = 0; i < n; i++) tet.insert(g(i));
  cout << n - 1 + (int)tet.size() + add;
  exit(0);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> b[i];
  int X = 0;
  for (int i = 0; i < n; i++) X ^= a[i];
  vector<int> t = get(a, b), q = get(b, a);
  if (t.size() >= 2) {
    cout << -1;
    return 0;
  }
  if (t.size() == 1 && q[0] != X) {
    cout << -1;
    return 0;
  }
  int tet = 0;
  if (t.size() == 1) {
    a.push_back(q[0]);
    b.push_back(t[0]);
    X = 0;
    tet = 1;
  }
  bool yes = false;
  for (int i = 0; i < b.size(); i++)
    if (b[i] == X) yes = true;
  solve(a, b, (int)!yes - tet);
}
