#include <bits/stdc++.h>
using namespace std;
const long long maxn = 200007;
long long n, a[maxn], b[maxn];
long long ans = 0;
inline long long read() {
  long long ret = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') ret = ret * 10 + ch - '0', ch = getchar();
  return ret * f;
}
namespace Number {
map<long long, long long> num;
long long cnt = 0;
inline void init() {
  num.clear();
  cnt = 0;
}
inline long long getnum(long long x) {
  if (num[x] == 0) {
    num[x] = ++cnt;
    return cnt;
  } else
    return num[x];
}
}  // namespace Number
namespace UniSet {
long long fa[maxn], sum[maxn];
inline void init(long long n) {
  for (long long i = 1; i <= n; i++) fa[i] = i, sum[i] = 1;
}
inline long long getfa(long long x) {
  if (fa[x] == x) return x;
  fa[x] = getfa(fa[x]);
  return fa[x];
}
inline void merge(long long x, long long y) {
  long long fx = getfa(x), fy = getfa(y);
  if (fx != fy) fa[fx] = fy, sum[fy] += sum[fx], sum[fx] = 0;
}
bool vis[maxn];
inline void count() {
  memset(vis, 0, sizeof(vis));
  for (long long i = 1; i <= Number::cnt; i++) {
    long long now = getfa(i);
    if (vis[now]) continue;
    vis[now] = true;
    if (sum[now] == 1) continue;
    ans++;
  }
  ans--;
}
}  // namespace UniSet
inline bool check() {
  sort(a + 1, a + 1 + n + 1);
  sort(b + 1, b + 1 + n + 1);
  for (long long i = 1; i <= n + 1; i++)
    if (a[i] != b[i]) return printf("-1\n"), false;
  return true;
}
signed main() {
  n = read();
  for (long long i = 1; i <= n; i++) a[i] = read(), a[n + 1] ^= a[i];
  for (long long i = 1; i <= n; i++)
    b[i] = read(), b[n + 1] ^= b[i], ans += a[i] != b[i];
  UniSet::init(n + 1);
  Number::init();
  for (long long i = 1; i <= n + 1; i++)
    UniSet::merge(Number::getnum(a[i]), Number::getnum(b[i]));
  if (!check()) return 0;
  UniSet::count();
  printf("%lld\n", ans < 0 ? 0 : ans);
  return 0;
}
