#include <bits/stdc++.h>
using namespace std;
const int MAXA = 3 * 1e5 + 7;
int n, cur[MAXA], c[MAXA << 1], a[MAXA], b[MAXA], cnt[MAXA], ans, suma;
vector<int> v[MAXA];
map<int, int> ma;
int dfs(int x, int st, int flag) {
  if (cur[x] == 0) {
    if (x == st || flag) return 1;
    return 0;
  }
  cur[x]--;
  if (!dfs(v[x][cur[x]], st, flag)) return 0;
  cnt[x]--;
  return 1;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    c[i] = a[i];
    suma ^= a[i];
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    c[n + i] = b[i];
  }
  c[2 * n + 1] = suma;
  sort(c + 1, c + n + n + 2);
  for (int i = 1; i <= 2 * n + 1; i++)
    if (c[i] != c[i - 1])
      ma[c[i]] = ma[c[i - 1]] + 1;
    else
      ma[c[i]] = ma[c[i - 1]];
  for (int i = 1; i <= n; i++) {
    if (a[i] == b[i]) continue;
    ans++;
    v[ma[b[i]]].push_back(ma[a[i]]);
    cnt[ma[b[i]]]++;
  }
  for (int i = 1; i <= n; i++) cur[ma[b[i]]] = v[ma[b[i]]].size();
  dfs(ma[suma], ma[suma], 1);
  for (int i = 1; i <= n; i++) {
    while (cnt[ma[b[i]]] && dfs(ma[b[i]], ma[b[i]], 0)) ans++;
    if (cnt[ma[b[i]]]) return printf("-1"), 0;
  }
  printf("%d", ans);
}
