#include <bits/stdc++.h>
using namespace std;
const long double Pi = 3.14159265358979323846l;
const int mod = 1e9 + 7;
inline int add(int x, int y) { return (x += y) - (x >= mod ? mod : 0); }
inline void inc(int &x, int y) { (x += y) -= (x >= mod ? mod : 0); }
inline int mul(int x, int y) { return 1ll * x * y % mod; }
inline int Power(int x, int y) {
  int res = 1;
  while (y) {
    if (y & 1) res = mul(res, x);
    x = mul(x, x), y >>= 1;
  }
  return res;
}
template <class T>
inline T input() {
  T x;
  char ch;
  while (!isdigit(ch = getchar()))
    ;
  for (x = ch ^ 48; isdigit(ch = getchar()); x = x * 10 + (ch ^ 48))
    ;
  return x;
}
template <class T>
inline void chkmin(T &x, T &y) {
  x = x < y ? x : y;
}
template <class T>
inline void chkmax(T &x, T &y) {
  x = x > y ? x : y;
}
struct {
  inline operator int() { return input<int>(); }
  inline operator long long() { return input<long long>(); }
  template <class T>
  inline void operator()(T &x) {
    x = *this;
  }
  template <class T, class... A>
  inline void operator()(T &x, A &...a) {
    x = *this;
    this->operator()(a...);
  }
} read;
int n, ans;
int a[100005], b[100005];
int tmpa[100005], tmpb[100005];
int date[100005 << 1], tot;
bool vis[100005 << 1];
int fa[100005 << 1];
inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
inline void merge(int x, int y) { fa[find(x)] = find(y); }
int main() {
  read(n);
  for (int i = 1; i <= n; ++i) read(a[i]), a[n + 1] ^= a[i];
  for (int i = 1; i <= n; ++i) read(b[i]), b[n + 1] ^= b[i];
  memcpy(tmpa, a, sizeof a);
  memcpy(tmpb, b, sizeof b);
  sort(tmpa + 1, tmpa + n + 2);
  sort(tmpb + 1, tmpb + n + 2);
  for (int i = 1; i <= n + 1; ++i)
    if (tmpa[i] != tmpb[i]) return puts("-1"), 0;
  for (int i = 1; i <= n; ++i) date[++tot] = a[i], date[++tot] = b[i];
  sort(date + 1, date + tot + 1);
  for (int i = 1; i <= n; ++i) {
    a[i] = lower_bound(date + 1, date + tot + 1, a[i]) - date;
    b[i] = lower_bound(date + 1, date + tot + 1, b[i]) - date;
  }
  for (int i = 1; i <= n << 1; ++i) fa[i] = i;
  for (int i = 1; i <= n + 1; ++i)
    if (a[i] != b[i]) {
      merge(a[i], b[i]);
      vis[a[i]] = true;
      vis[b[i]] = true;
      ++ans;
    }
  vis[a[n + 1]] = true;
  if (!ans) return puts("0"), 0;
  for (int i = 1; i <= n << 1; ++i)
    if (vis[i] && fa[i] == i) ++ans;
  printf("%d\n", ans - 1);
  return 0;
}
