#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, k, ans, u, v;
int a[100005], b[100005], a_[100005], b_[100005];
int fa[100005], cnt[100005];
map<int, int> Hash;
int get(int x) { return fa[x] == x ? x : fa[x] = get(fa[x]); }
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; ++i) scanf("%d", &a[i]), a[0] ^= a[i];
  for (i = 1; i <= n; ++i) scanf("%d", &b[i]), b[0] ^= b[i];
  for (i = 0; i <= n; ++i) a_[i] = a[i], b_[i] = b[i];
  sort(a_, a_ + n + 1);
  sort(b_, b_ + n + 1);
  for (i = 0; i <= n; ++i)
    if (a_[i] != b_[i]) {
      printf("-1\n");
      return 0;
    }
  for (i = 1; i <= n; ++i) {
    if (!Hash[a[i]]) Hash[a[i]] = ++m;
    if (!Hash[b[i]]) Hash[b[i]] = ++m;
    a[i] = Hash[a[i]];
    b[i] = Hash[b[i]];
  }
  for (i = 1; i <= m; ++i) fa[i] = i;
  for (i = 0; i <= n; ++i)
    if (a[i] != b[i]) {
      ++ans;
      ++cnt[a[i]];
      ++cnt[b[i]];
      u = get(a[i]);
      v = get(b[i]);
      if (rand() % 2) swap(u, v);
      fa[u] = v;
    }
  for (i = 1; i <= m; ++i)
    if (cnt[i] && fa[i] == i) ++ans;
  if (a[0] != b[0]) --ans;
  --ans;
  if (ans < 0) ans = 0;
  printf("%d\n", ans);
}
