#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, m, tot;
int a[N], b[N], fa[N], siz[N];
int c[N], d[N], num[N];
int find(int x) { return (fa[x] == x) ? x : fa[x] = find(fa[x]); }
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), c[i] = a[i], a[0] ^= a[i];
  for (int i = 1; i <= n; ++i) scanf("%d", &b[i]), d[i] = b[i], b[0] ^= b[i];
  c[0] = a[0], d[0] = b[0];
  sort(c, c + n + 1);
  sort(d, d + n + 1);
  for (int i = 0; i <= n; ++i)
    if (c[i] != d[i]) return 0 * puts("-1");
  int flag = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] != b[i]) flag = 1;
  }
  if (!flag) return 0 * puts("0");
  for (int i = 0; i <= n; ++i) {
    num[++tot] = a[i];
    num[++tot] = b[i];
  }
  sort(num + 1, num + tot + 1);
  tot = unique(num + 1, num + tot + 1) - num - 1;
  int ans = 0;
  for (int i = 0; i <= n; ++i) {
    if (a[i] == b[i]) continue;
    a[i] = lower_bound(num + 1, num + tot + 1, a[i]) - num - 1;
    b[i] = lower_bound(num + 1, num + tot + 1, b[i]) - num - 1;
    if (!fa[a[i]]) {
      ++ans;
      fa[a[i]] = a[i];
    }
    if (!fa[b[i]]) {
      ++ans;
      fa[b[i]] = b[i];
    }
    if (a[i] != b[i]) {
      if (i) ++ans;
      if (find(a[i]) != find(b[i])) {
        --ans;
        fa[find(a[i])] = find(b[i]);
      }
    }
  }
  cout << ans - 1 << endl;
  return 0;
}
