#include <bits/stdc++.h>
using namespace std;
long long n, a[100005], l[100005], b[100005];
long long f, fa[100005], s1, s2, a1[100005], l1[100005], son[100005],
    a2[100005], l2[100005], cnt, m1, m2, ans, k;
long long find(long long x) {
  if (x != fa[x]) fa[x] = find(fa[x]);
  return fa[x];
}
int main() {
  scanf("%lld", &n);
  for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]), a1[i] = a[i];
  for (long long i = 1; i <= n; i++) scanf("%lld", &l[i]), l1[i] = l[i];
  for (long long i = 1; i <= n; i++) s1 ^= a[i], s2 ^= l[i];
  a[n + 1] = s1;
  l[n + 1] = s2;
  a1[n + 1] = s1;
  l1[n + 1] = s2;
  sort(a1 + 1, a1 + n + 2);
  sort(l1 + 1, l1 + n + 2);
  for (long long i = 1; i <= n + 1; i++)
    if (a1[i] != l1[i]) {
      puts("-1");
      return 0;
    }
  memset(a1, 0, sizeof(a1));
  memset(l1, 0, sizeof(l1));
  for (long long i = 1; i <= n + 1; i++)
    if (a[i] != l[i]) {
      ++cnt;
      a1[cnt] = a[i];
      a2[cnt] = a[i];
      l1[cnt] = l[i];
      l2[cnt] = l[i];
    }
  sort(a2 + 1, a2 + cnt + 1);
  sort(l2 + 1, l2 + cnt + 1);
  m1 = unique(a2 + 1, a2 + cnt + 1) - a2 - 1;
  m2 = unique(l2 + 1, l2 + cnt + 1) - l2 - 1;
  for (long long i = 1; i <= cnt; i++) {
    a1[i] = lower_bound(a2 + 1, a2 + m1 + 1, a1[i]) - a2;
    l1[i] = lower_bound(l2 + 1, l2 + m2 + 1, l1[i]) - l2;
  }
  for (long long i = 1; i <= cnt; i++) fa[i] = i, son[i] = 1;
  for (long long i = 1; i <= cnt; i++) {
    long long x = find(a1[i]), y = find(l1[i]);
    if (x != y) {
      fa[x] = y;
      son[y] += son[x];
      b[x] = 1;
      b[y] = 1;
    }
  }
  for (long long i = 1; i <= cnt; i++)
    if (fa[i] == i) k++, ans += son[i];
  if (ans + k - 1 > 0) printf("%lld", ans + k - 1);
  return 0;
}
