#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, a[N], b[N], x = 0, sum = 0, tag = 0, ans = 0, shit = 0;
map<long long, int> mp;
struct data {
  int pos, val;
};
bool operator<(data x, data y) {
  if (x.val == y.val) return x.pos < y.pos;
  return x.val < y.val;
}
set<data> s;
set<data>::iterator it;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return f * x;
}
inline void write(int x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
  return;
}
inline void no() {
  printf("-1\n");
  exit(0);
}
int main() {
  n = read();
  for (register int i = (1); i <= (n); i++)
    a[i] = read(), x ^= a[i], mp[a[i]]++, sum++;
  mp[x]++, sum++;
  for (register int i = (1); i <= (n); i++) {
    b[i] = read();
    if (mp[b[i]]) sum--, mp[b[i]]--;
  }
  if (sum != 1) no();
  for (register int i = (1); i <= (n); i++)
    if (a[i] != b[i]) s.insert((data){i, b[i]}), shit++;
  while (shit) {
    it = s.lower_bound((data){0, x});
    if (it == s.end()) it = s.begin();
    if ((*it).val == x) {
      shit--;
      swap(a[(*it).pos], x);
      s.erase(it);
      ans++;
    } else {
      ans++;
      swap(a[(*it).pos], x);
    }
  }
  write(ans);
  return 0;
}
