#include <bits/stdc++.h>
using namespace ::std;
const int MAXN = 1e5 + 5;
int N;
vector<queue<int> > goals(MAXN);
int COMP = 1;
map<int, int> compress;
map<int, int> elements_a;
int union_count = 0;
struct DSU {
  vector<int> parent;
  vector<int> rank;
  DSU(int n) {
    parent.resize(n, 0);
    for (int i = 0; i < n; i++) {
      parent[i] = i;
    }
    rank.resize(n, 0);
  }
  int get_root(int cur) {
    if (parent[cur] == cur) {
      return cur;
    }
    return parent[cur] = get_root(parent[cur]);
  }
  void union_(int u, int v) {
    u = get_root(u);
    v = get_root(v);
    if (u == v) {
      return;
    }
    union_count++;
    if (rank[u] < rank[v]) {
      parent[u] = v;
    } else {
      parent[v] = u;
      if (rank[u] == rank[v]) {
        rank[u]++;
      }
    }
  }
};
DSU dsu(MAXN);
int main() {
  ios::sync_with_stdio(false);
  ifstream fin("input.txt");
  cin >> N;
  vector<int> a(N), b(N);
  int tot = 0;
  int a_tot_cnt = 0;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    tot ^= a[i];
    elements_a[a[i]]++;
    if (compress.count(a[i]) == 0) {
      compress[a[i]] = COMP;
      COMP++;
    }
    a[i] = compress[a[i]];
  }
  elements_a[tot]++;
  if (compress.count(tot) == 0) {
    compress[tot] = COMP;
    COMP++;
  }
  tot = compress[tot];
  for (int i = 0; i < N; i++) {
    cin >> b[i];
    elements_a[b[i]]--;
    if (elements_a[b[i]] < 0) {
      cout << "-1";
      return 0;
    }
    if (compress.count(b[i]) == 0) {
      compress[b[i]] = COMP;
      COMP++;
    }
    b[i] = compress[b[i]];
  }
  bool tot_exist = -1;
  vector<int> go_to(N, -1);
  int cnt = 0;
  int singles = 0;
  for (int i = 0; i < N; i++) {
    if (a[i] == b[i]) {
      singles++;
      continue;
    }
    if (b[i] == tot) {
      tot_exist++;
    }
    dsu.union_(a[i], b[i]);
    cnt++;
  }
  int group_count = COMP - 1 - union_count - singles;
  cout << cnt + group_count - 1;
}
