#include <bits/stdc++.h>
using namespace std;
int n, a[100010], b[100010], x[200010], p, q;
map<int, int> h;
vector<int> w[200010], A;
bool u[200010];
inline int dfs(int i) {
  int j, k = 1;
  u[i] = 1;
  for (j = 0; j < w[i].size(); j++)
    if (!u[w[i][j]]) k += dfs(w[i][j]);
  return k;
}
int main() {
  int i, j, k = 0, l;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (i = 1; i <= n; i++) scanf("%d", &b[i]);
  for (i = 1; i <= n; i++) {
    k ^= a[i];
    if (!h[a[i]]) h[a[i]] = ++p;
    a[i] = h[a[i]];
    if (!h[b[i]]) h[b[i]] = ++p;
    b[i] = h[b[i]];
    w[a[i]].push_back(b[i]);
    x[a[i]]++;
    x[b[i]]--;
  }
  if (!h[k]) h[k] = ++p;
  k = h[k];
  for (i = 1, j = 0; i <= p; i++)
    if (x[i] > 0) j += x[i];
  if (j > 1) {
    printf("-1\n");
    return 0;
  }
  if (j == 1) {
    for (i = 1; i <= p; i++)
      if (x[i] == 1)
        j = i;
      else if (x[i] == -1)
        l = i;
    for (i = 0; i < w[j].size(); i++)
      if (w[j][i] == l) {
        w[j][i] = j;
        break;
      }
    w[k].push_back(l);
  }
  q = -1;
  for (i = 1; i <= p; i++)
    if (!u[i]) {
      l = dfs(i);
      if (l > 1)
        q += l + 1;
      else if (i == k) {
        q++;
        if (j) q++;
      }
    }
  printf("%d\n", q);
  return 0;
}
