#include <bits/stdc++.h>
using namespace std;
int N;
int V;
int A[100001], B[100001];
int K;
int U[100001], R[100001];
vector<int> G[100001];
int find(int x) {
  if (U[x] == x) return x;
  return U[x] = find(U[x]);
}
void unite(int x, int y) {
  x = find(x), y = find(y);
  if (x == y) return;
  if (R[x] < R[y]) swap(x, y);
  U[y] = x;
  R[x] += R[y];
  R[y] = 0;
  K--;
}
bool same(int x, int y) { return find(x) == find(y); }
void add_edge(int x, int y) {
  G[x].push_back(y);
  G[y].push_back(x);
  unite(x, y);
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  int g = 0;
  for (int i = 0; i < (N); i++) {
    cin >> A[i];
    g ^= A[i];
  }
  A[N] = g;
  g = 0;
  for (int i = 0; i < (N); i++) {
    cin >> B[i];
    g ^= B[i];
  }
  B[N] = g;
  vector<int> as, bs;
  for (int i = 0; i < (N + 1); i++) {
    as.push_back(A[i]);
    bs.push_back(B[i]);
  }
  sort((as).begin(), (as).end());
  sort((bs).begin(), (bs).end());
  if (as != bs) {
    cout << -1 << "\n";
    return 0;
  }
  for (int i = 0; i < (N + 1); i++) assert(as[i] == bs[i]);
  as.erase(unique((as).begin(), (as).end()), (as).end());
  V = as.size();
  for (int i = 0; i < (N + 1); i++) {
    A[i] = lower_bound((as).begin(), (as).end(), A[i]) - as.begin();
    B[i] = lower_bound((as).begin(), (as).end(), B[i]) - as.begin();
  }
  K = V;
  for (int i = 0; i < (V); i++) U[i] = i, R[i] = 1;
  int edges = 0;
  for (int i = 0; i < (N); i++) {
    if (A[i] == B[i]) continue;
    add_edge(A[i], B[i]);
    edges++;
  }
  int k = 0;
  for (int i = 0; i < (V); i++)
    if (G[i].empty()) k++;
  int s = B[N], t = A[N];
  assert(same(s, t));
  if (K - k <= 1) {
    cout << edges << "\n";
  } else {
    cout << edges + K - 1 << "\n";
  }
  return 0;
}
