#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int a[MAXN], unq[MAXN], sz, b[MAXN];
int out[MAXN], in[MAXN], par[MAXN];
int root(int v) { return par[v] = (v == par[v] ? v : root(par[v])); }
void merge(int v, int u) {
  v = root(v);
  u = root(u);
  if (v == u) return;
  par[v] = u;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, x = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    x ^= a[i];
    unq[sz++] = a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    unq[sz++] = b[i];
  }
  unq[sz++] = x;
  sort(unq, unq + sz);
  sz = unique(unq, unq + sz) - unq;
  for (int i = 0; i < sz; i++) par[i] = i;
  int ans = n, con = sz;
  for (int i = 0; i < n; i++) {
    int u = lower_bound(unq, unq + sz, a[i]) - unq;
    int v = lower_bound(unq, unq + sz, b[i]) - unq;
    if (v == u) {
      ans--;
      con--;
    }
    out[v]++;
    in[u]++;
    if (root(v) != root(u)) {
      con--;
    }
    merge(v, u);
  }
  int f1 = 0, f2 = 0;
  for (int i = 0; i < sz; i++) {
    if (out[i] > in[i]) {
      if (f1 || out[i] - in[i] > 1) {
        cout << "-1\n";
        return 0;
      }
      f1 = 1;
    } else if (out[i] < in[i]) {
      if (f2 || in[i] - out[i] > 1) {
        cout << "-1\n";
        return 0;
      }
      f2 = 1;
    }
  }
  if (f1 && f2 == 0) {
    cout << "-1\n";
    return 0;
  }
  if (f1 == 0 && f2) {
    cout << "-1\n";
    return 0;
  }
  cout << ans + con - 1 << "\n";
}
