#include <bits/stdc++.h>
using namespace std;
vector<int> G[200005];
int n, m, a[200005], b[200005], c[200005], d[200005], w[200005], lw,
    vis[200005], ans;
int main() {
  int i, j, x, cnt;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]), a[n + 1] ^= a[i];
  for (i = 1; i <= n; i++) scanf("%d", &b[i]), b[n + 1] ^= b[i];
  n++;
  for (i = 1; i <= n; i++)
    if (a[i] != b[i]) ++m, c[m] = a[i], d[m] = b[i];
  if (a[n] != b[n])
    ans -= 2;
  else {
    for (i = 1; i <= m; i++)
      if (c[i] == a[n]) break;
    ans--;
  }
  sort(a + 1, a + n + 1);
  sort(b + 1, b + n + 1);
  for (i = 1; i <= n; i++)
    if (a[i] != b[i]) {
      printf("-1");
      return 0;
    }
  for (i = 1; i <= m; i++) w[++lw] = c[i], w[++lw] = d[i];
  sort(w + 1, w + lw + 1);
  for (i = 1; i <= m; i++) {
    c[i] = lower_bound(w + 1, w + lw + 1, c[i]) - w;
    d[i] = lower_bound(w + 1, w + lw + 1, d[i]) - w;
  }
  for (i = 1; i <= m; i++) G[c[i]].push_back(i);
  for (i = 1; i <= m; i++) {
    if (vis[i]) continue;
    for (j = i, cnt = 0; !vis[j]; j = x) {
      cnt++, x = G[d[j]][G[d[j]].size() - 1];
      G[d[j]].pop_back(), vis[j] = 1;
    }
    ans += cnt + 1;
  }
  cout << ans;
  return 0;
}
