#include <bits/stdc++.h>
inline int read() {
  int x = 0;
  char ch = getchar();
  for (; ch < 48 || ch > 57; ch = getchar())
    ;
  for (; ch > 47 && ch < 58; ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ 48);
  return x;
}
using namespace std;
int s[201000], t[201000], num[201000], vis[201000], sum, ans;
queue<int> Q[201000];
int get(int x) {
  for (int cnt = 0;; x = Q[sum].front()) {
    vis[x] = 1;
    sum = s[x];
    ++cnt;
    for (; !Q[sum].empty() && vis[Q[sum].front()];) Q[sum].pop();
    if (Q[sum].empty()) return cnt;
  }
}
int main() {
  int n = read();
  for (int i = 1; i <= n; ++i) sum ^= (num[i] = s[i] = read());
  for (int i = 1; i <= n; ++i) num[i + n] = t[i] = read();
  num[n << 1 | 1] = sum;
  sort(num + 1, num + n + n + 2);
  int m = unique(num + 1, num + n + n + 2) - num - 1;
  for (int i = 1; i <= n; ++i)
    ++vis[s[i] = lower_bound(num + 1, num + m + 1, s[i]) - num],
        --vis[t[i] = lower_bound(num + 1, num + m + 1, t[i]) - num];
  ++vis[sum = lower_bound(num + 1, num + m + 1, sum) - num];
  for (int i = 1; i <= m; ++i)
    if (vis[i] < 0) return puts("-1"), 0;
  memset(vis, 0, sizeof vis);
  for (int i = 1; i <= n; ++i)
    if (s[i] ^ t[i])
      Q[t[i]].push(i);
    else
      vis[i] = 1;
  if (Q[sum].size()) ans += get(Q[sum].front());
  for (int i = 1; i <= n; ++i)
    if (!vis[i] && Q[t[i]].size()) ans += get(i) + 1;
  printf("%d\n", ans);
  return 0;
}
