#include <bits/stdc++.h>
long long MOD = 1e9 + 7;
using namespace std;
string alphabet("abcdefghijklmnopqrstuvwxyz");
int par[200000], grade[200000];
void init(int n) {
  for (int i = 0; i < n + 1; i++) {
    par[i] = i;
    grade[i] = 0;
  }
}
int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); }
bool same(int x, int y) { return root(x) == root(y); }
void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y) return;
  if (grade[x] < grade[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (grade[x] == grade[y]) grade[x]++;
  }
}
int main() {
  map<long long, int> m;
  int num = 0, n;
  long long x = 0;
  cin >> n;
  long long a[200000] = {};
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    x ^= a[i];
    if (m[a[i]])
      m[a[i]]++;
    else {
      m[a[i]] = 1;
      num++;
    }
  }
  if (m[x])
    m[x]++;
  else {
    m[x] = 1;
    num++;
  }
  init(num);
  int ans = n;
  long long b[200000] = {};
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    if (a[i] == b[i]) {
      ans -= 2;
      if (m[a[i]] == 0) {
        cout << -1;
        return 0;
      }
      m[a[i]]--;
    } else {
      if (m[b[i]] == 0) {
        cout << -1;
        return 0;
      }
      m[b[i]]--;
      int s = distance(begin(m), m.find(a[i])),
          t = distance(begin(m), m.find(b[i]));
      unite(s, t);
    }
  }
  if (ans == -n) {
    cout << 0;
    return 0;
  }
  for (int i = 0; i < num; i++) {
    if (par[i] == i) ans++;
  }
  cout << --ans;
  return 0;
}
