#include <bits/stdc++.h>
using namespace std;
int n, a[400005], b[400005], cnt, to[400005], nxt[400005], fst[400005], l,
    vis[400005], Ans, s[400005], s1[400005];
map<int, int> Map;
void link(int x, int y) {
  to[++l] = y;
  nxt[l] = fst[x];
  fst[x] = l;
  to[++l] = x;
  nxt[l] = fst[y];
  fst[y] = l;
}
void dfs(int x) {
  vis[x] = 0;
  for (int i = fst[x]; i; i = nxt[i])
    if (vis[to[i]]) dfs(to[i]);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), a[0] ^= a[i];
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]), b[0] ^= b[i];
  for (int i = 0; i <= n; i++) {
    if (!Map[a[i]]) Map[a[i]] = ++cnt;
    if (!Map[b[i]]) Map[b[i]] = ++cnt;
    a[i] = Map[a[i]];
    b[i] = Map[b[i]];
    s[a[i]]++;
    s1[b[i]]++;
  }
  for (int i = 1; i <= cnt; i++)
    if (s[i] != s1[i]) {
      puts("-1");
      return 0;
    }
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) {
      vis[a[i]] = vis[b[i]] = 1;
      link(a[i], b[i]);
      Ans++;
    }
  if (a[0] != b[0] || a[0] == b[0] && s[a[0]] > 1) Ans--;
  for (int i = 1; i <= cnt; i++)
    if (vis[i]) {
      dfs(i);
      Ans++;
    }
  printf("%d\n", Ans);
}
