#include <bits/stdc++.h>
using namespace std;
int n, m, sh[400005], a[400005], b[400005], num[400005], fa[400005],
    vis[400005];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
  cin >> n;
  int sum = 0;
  for (int i = 1; i <= n; ++i) scanf("%d", a + i), sh[++m] = a[i], sum ^= a[i];
  for (int i = 1; i <= n; ++i) scanf("%d", b + i), sh[++m] = b[i];
  a[++n] = sh[++m] = sum;
  sort(sh + 1, sh + m + 1);
  m = unique(sh + 1, sh + m + 1) - (sh + 1);
  for (int i = 1; i <= n; ++i)
    ++num[a[i] = lower_bound(sh + 1, sh + m + 1, a[i]) - sh];
  for (int i = 1; i < n; ++i)
    --num[b[i] = lower_bound(sh + 1, sh + m + 1, b[i]) - sh];
  for (int i = 1; i <= m; ++i)
    if (num[i]) {
      if (b[n])
        return puts("-1"), 0;
      else
        b[n] = i;
    }
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i]) vis[a[i]] = 1, vis[b[i]] = 1;
  for (int i = 1; i <= m; ++i)
    if (vis[i]) fa[i] = i;
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] == b[i]) continue;
    if (i < n) ++ans;
    if (find(a[i]) != find(b[i])) fa[find(a[i])] = find(b[i]);
  }
  for (int i = 1; i <= m; ++i) ans += (fa[i] == i);
  if (!ans)
    puts("0");
  else
    cout << ans - 1 << endl;
  return 0;
}
