#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
inline long long getint() {
  long long _x = 0, _tmp = 1;
  char _tc = getchar();
  while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
  if (_tc == '-') _tc = getchar(), _tmp = -1;
  while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
  return _x * _tmp;
}
inline long long add(long long _x, long long _y,
                     long long _mod = 1000000007LL) {
  _x += _y;
  return _x >= _mod ? _x - _mod : _x;
}
inline long long sub(long long _x, long long _y,
                     long long _mod = 1000000007LL) {
  _x -= _y;
  return _x < 0 ? _x + _mod : _x;
}
inline long long mul(long long _x, long long _y,
                     long long _mod = 1000000007LL) {
  _x *= _y;
  return _x >= _mod ? _x % _mod : _x;
}
long long mypow(long long _a, long long _x, long long _mod) {
  if (_x == 0) return 1LL;
  long long _ret = mypow(mul(_a, _a, _mod), _x >> 1, _mod);
  if (_x & 1) _ret = mul(_ret, _a, _mod);
  return _ret;
}
long long mymul(long long _a, long long _x, long long _mod) {
  if (_x == 0) return 0LL;
  long long _ret = mymul(add(_a, _a, _mod), _x >> 1, _mod);
  if (_x & 1) _ret = add(_ret, _a, _mod);
  return _ret;
}
inline bool equal(double _x, double _y) {
  return _x > _y - 1e-9 && _x < _y + 1e-9;
}
int __ = 1, _cs;
void build() {}
int n, a[101010], b[101010], ok, x;
void init() {
  n = getint();
  for (int i = 0; i < n; i++) a[i] = getint();
  for (int i = 0; i < n; i++) b[i] = getint();
}
map<int, int> indeg;
map<int, vector<int>> nxt;
int cnt;
void go(int st) {
  if (nxt[st].empty()) return;
  cnt++;
  while (nxt[st].size()) {
    int tmp = nxt[st].back();
    nxt[st].pop_back();
    st = tmp;
  }
}
void find_ans() {
  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) continue;
    indeg[a[i]]++;
    nxt[b[i]].push_back(a[i]);
  }
  for (int i = 0; i < n; i++)
    if (indeg.count(b[i]) == 0) go(b[i]);
  for (int i = 0; i < n; i++)
    while (nxt[b[i]].size()) go(b[i]);
  int found = 0;
  for (int i = 0; i < n; i++)
    if (a[i] != b[i] and b[i] == x) found = 1;
  printf("%d\n", n - ok + cnt - found);
}
void solve() {
  for (int i = 0; i < n; i++)
    if (a[i] == b[i]) ok++;
  if (ok == n) {
    puts("0");
    exit(0);
  }
  map<int, int> M;
  for (int i = 0; i < n; i++) M[b[i]]++;
  for (int i = 0; i < n; i++) {
    if (M.count(a[i])) {
      M[a[i]]--;
      if (M[a[i]] == 0) M.erase(a[i]);
    }
    x = x xor a[i];
  }
  if (M.count(x)) {
    M[x]--;
    if (M[x] == 0) M.erase(x);
  }
  if (M.size()) {
    puts("-1");
    exit(0);
  }
  find_ans();
}
int main() {
  build();
  while (__--) {
    init();
    solve();
  }
}
