#include <bits/stdc++.h>
using namespace std;
int a[110000], b[110000], n, A[110000], B[110000], s, now, ans;
map<int, vector<int> > ma;
map<int, int> head;
bool v[110000];
bool check() {
  for (int i = 1; i <= n; i++) A[i] = a[i], B[i] = b[i];
  A[n + 1] = s;
  sort(A + 1, A + n + 2);
  sort(B + 1, B + n + 1);
  int j = 1;
  for (int i = 1; i <= n; i++) {
    while (j <= n + 1 && B[i] > A[j]) j++;
    if (j > n + 1 || B[i] < A[j]) return 0;
    j++;
  }
  return 1;
}
void dfs(int x) {
  v[x] = 1;
  int siz = ma[b[x]].size();
  while (head[b[x]] < siz && v[ma[b[x]][head[b[x]]]]) head[b[x]]++;
  if (head[b[x]] < siz) dfs(ma[b[x]][head[b[x]]]);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), s ^= a[i];
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  int m = 0;
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) A[++m] = a[i], B[m] = b[i];
  n = m;
  for (int i = 1; i <= n; i++) a[i] = A[i], b[i] = B[i];
  if (check() == 0) {
    printf("-1\n");
    return 0;
  }
  ans = n;
  for (int i = 1; i <= n; i++)
    if (b[i] == s) {
      ans--;
      a[i] = s;
      break;
    }
  for (int i = 1; i <= n; i++)
    if (b[i] == s) {
      ans--;
      a[i] = s;
      break;
    }
  for (int i = 1; i <= n; i++) ma[a[i]].push_back(i), head[a[i]] = 0;
  for (int i = 1; i <= n; i++) {
    if (v[i]) continue;
    ans++;
    dfs(i);
  }
  printf("%d\n", ans);
  return 0;
}
