#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, k, x, ans, u, v, tot, cnt;
int a[100005], b[100005];
int son[200005], Next[200005], ed[200005];
int init[200005], outit[200005];
int fa[200005];
map<int, int> Hash;
void fail() {
  printf("-1\n");
  exit(0);
}
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]), x ^= a[i];
  for (i = 1; i <= n; ++i) scanf("%d", &b[i]);
  for (i = 1; i <= n; ++i)
    if (a[i] != b[i]) break;
  if (i > n) {
    printf("0\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 = 1; i <= n; ++i)
    if (a[i] != b[i]) {
      ++ans;
      u = a[i];
      v = b[i];
      ++tot;
      Next[tot] = son[u];
      son[u] = tot;
      ed[tot] = v;
      ++init[v];
      ++outit[u];
      u = get(u);
      v = get(v);
      fa[u] = v;
    }
  if (!Hash[x]) fail();
  x = Hash[x];
  for (i = 1; i <= m; ++i)
    if (init[i] || outit[i]) {
      if (get(i) != get(x)) {
        if (fa[i] == i) ++ans;
        if (init[i] != outit[i]) fail();
      }
      if (init[i] != outit[i]) ++cnt;
    }
  if (cnt && outit[x] + 1 != init[x]) fail();
  printf("%d\n", ans);
}
