#include <bits/stdc++.h>
using namespace std;
const int N = 1E5 + 50;
int n, m, ans, vis[N], a[N], b[N], c[N], d[N], f[N];
int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); }
void merge(int u, int v) { f[u] = v; }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i), m ^= a[i], c[i] = a[i], f[i] = i;
  c[++n] = a[n] = m, m = 0;
  for (int i = 1; i < n; i++) scanf("%d", b + i), m ^= b[i], d[i] = b[i];
  d[n] = b[n] = m;
  sort(c + 1, c + 1 + n), sort(d + 1, d + 1 + n);
  for (int i = 1; i <= n; i++) {
    if (c[i] != d[i]) return 0 * printf("-1\n");
  }
  int len = unique(c + 1, c + 1 + n) - c - 1;
  for (int i = 1; i <= n; i++) {
    a[i] = lower_bound(c + 1, c + 1 + len, a[i]) - c;
    b[i] = lower_bound(c + 1, c + 1 + len, b[i]) - c;
    if (a[i] != b[i]) {
      vis[a[i]] = vis[b[i]] = 1;
      merge(find(a[i]), find(b[i]));
      ans++;
    }
  }
  for (int i = 1; i <= len; i++) {
    find(i);
    if (!vis[i]) ans--;
  }
  sort(f + 1, f + 1 + len);
  ans += unique(f + 1, f + 1 + len) - f - 1;
  cout << ans - 1 - (a[n] != b[n]) << '\n';
  return 0;
}
