#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
const long long INF = 2147483600;
const long long MAXN = 300010;
long long N;
long long a[MAXN + 1], cnt, d[MAXN + 1], b[MAXN + 1], c[MAXN + 1];
long long Pos;
long long T[MAXN + 1];
vector<long long> vec[MAXN + 1];
bool flag;
bool vis[MAXN + 1];
int main() {
  N = read();
  long long num = 0, tg = 0;
  for (long long i = 1; i <= N; i++) a[i] = read(), num = num ^ a[i];
  for (long long i = 1; i <= N; i++)
    b[i] = read(), c[++tg] = a[i], c[++tg] = b[i];
  c[++tg] = num;
  sort(c + 1, c + tg + 1);
  flag = true;
  for (long long i = 1; i <= N; i++)
    if (a[i] != b[i]) {
      flag = false;
      break;
    }
  if (flag) {
    puts("0");
    return 0;
  }
  for (long long i = 1; i <= N; i++) {
    long long x = lower_bound(c + 1, c + tg + 1, a[i]) - c;
    T[x]++;
  }
  for (long long i = 1; i <= N; i++) {
    long long x = lower_bound(c + 1, c + tg + 1, b[i]) - c;
    T[x]--;
  }
  T[lower_bound(c + 1, c + tg + 1, num) - c]++;
  for (long long i = 1; i <= tg; i++)
    if (T[i] < 0) {
      puts("-1");
      return 0;
    }
  for (long long i = 1; i <= N; i++) {
    if (b[i] == num && T[lower_bound(c + 1, c + tg + 1, b[i]) - c]) {
      Pos = i;
      break;
    }
  }
  if (!Pos)
    for (long long i = 1; i <= N; i++) {
      if (b[i] == num) {
        Pos = i;
        break;
      }
    }
  if (!Pos) {
    puts("-1");
    return 0;
  }
  for (long long i = 1; i <= N; i++)
    if (i != Pos) {
      a[i] = lower_bound(c + 1, c + tg + 1, a[i]) - c;
      b[i] = lower_bound(c + 1, c + tg + 1, b[i]) - c;
    }
  a[Pos] = b[Pos];
  long long tot1 = 0, tot2 = 0, tot = 0;
  for (long long i = 1; i <= N; i++) {
    if (a[i] == b[i]) a[i] = b[i] = INF;
  }
  memset(T, 0, sizeof(T));
  Pos = -INF;
  long long ans = 0;
  for (long long i = 1; i <= N; i++)
    if (a[i] != b[i]) ans += 3;
  ans -= 3;
  ans++;
  for (long long i = 1; i <= N; i++) {
    if (a[i] != INF) T[a[i]]++;
    if (b[i] != INF) T[b[i]]--;
  }
  for (long long i = 1; i <= tg; i++)
    if (T[i] < 0) ans += -T[i], Pos = i;
  for (long long i = 1; i <= N; i++)
    if (b[i] == Pos) a[i] = b[i];
  for (long long i = 1; i <= N; i++)
    if (a[i] != b[i]) c[++tot1] = a[i], d[++tot2] = b[i];
  for (long long i = 1; i <= tot1; i++) a[i] = c[i], b[i] = d[i];
  tot = tot1;
  for (long long i = 1; i <= tot1; i++) vec[a[i]].push_back(i);
  for (long long i = 1; i <= tot1; i++) {
    if (!vis[i]) {
      for (long long j = 0; j < vec[b[i]].size(); j++) {
        if (b[vec[b[i]][j]] == a[i] && !vis[vec[b[i]][j]]) {
          flag = true;
          vis[vec[b[i]][j]] = true;
          break;
        }
      }
      if (flag) ans -= 3;
    }
  }
  printf("%d\n", ans);
  return 0;
}
