#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, a[N], b[N], c[N], d[N], p[N], l;
map<int, int> val;
int f[N];
inline int find(int first) {
  return first == f[first] ? first : f[first] = find(f[first]);
}
void merge(int a, int b) {
  a = find(a), b = find(b);
  if (a != b) f[a] = b;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", a + i);
  for (int i = 1; i <= n; i++) scanf("%d", b + i);
  a[n + 1] = 0;
  for (int i = 1; i <= n; i++) a[n + 1] ^= a[i];
  b[n + 1] = 0;
  for (int i = 1; i <= n; i++) b[n + 1] ^= b[i];
  n++;
  memcpy(c + 1, a + 1, n * sizeof(a[0]));
  memcpy(d + 1, b + 1, n * sizeof(b[0]));
  sort(c + 1, c + n + 1);
  sort(d + 1, d + n + 1);
  for (int i = 1; i <= n; i++)
    if (c[i] != d[i]) {
      printf("-1\n");
      return 0;
    }
  l = unique(c + 1, c + n + 1) - c - 1;
  for (int i = 1; i <= l; i++) val[c[i]] = i;
  for (int i = 1; i <= n; i++) a[i] = val[a[i]], b[i] = val[b[i]];
  static vector<int> pos[N];
  for (int i = 1; i <= n; i++) pos[b[i]].push_back(i);
  for (int i = 1; i <= n; i++) {
    p[i] = *pos[a[i]].rbegin();
    pos[a[i]].pop_back();
  }
  for (int i = 1; i <= n; i++) f[i] = i;
  static bool vis[N];
  for (int i = 1; i <= n; i++)
    if (!vis[i]) {
      vis[i] = true;
      for (int j = p[i]; j != i; j = p[j]) vis[j] = true, merge(j, i);
    }
  for (int i = 1; i <= n; i++) pos[a[i]].push_back(i);
  for (int i = 1; i <= n; i++)
    if (pos[i].size()) {
      int len = pos[i].size();
      for (int j = 1; j <= len - 1; j++) merge(pos[i][j], pos[i][j - 1]);
    }
  static int cnt[N];
  for (int i = 1; i <= n; i++) cnt[find(i)]++;
  int ans = 0;
  for (int i = 1; i <= n; i++)
    if (f[i] == i) {
      if (i == find(n))
        ans += cnt[i] - 1;
      else if (cnt[i] > 1)
        ans += cnt[i] + 1;
    }
  printf("%d\n", ans);
  return 0;
}
