#include <bits/stdc++.h>
using namespace std;
int A[100001], B[100001];
vector<pair<int, int> > C;
vector<int> Next(C.size());
int NXT(int q) {
  int ans = q;
  while (Next[ans] != ans) ans = Next[ans];
  while (q != ans) {
    int w = Next[q];
    Next[q] = ans;
    q = w;
  }
  return ans;
}
int fnd(int q) {
  int l = 0, r = C.size() - 1;
  while (r > l) {
    int m = (r + l) / 2;
    if (C[m].first == q)
      return NXT(m);
    else if (C[m].first < q)
      l = m + 1;
    else
      r = m - 1;
  }
  if (l == r)
    return NXT(l);
  else
    return NXT(0);
}
bool RM(int q) {
  NXT((q + 1) % (C.size()));
  if (Next[(q + 1) % (C.size())] == q) return true;
  Next[q] = (q + 1) % (C.size());
  return false;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &A[i]);
  for (int i = 0; i < n; i++) scanf("%d", &B[i]);
  int N = 0;
  for (int i = 0; i < n; i++) N ^= A[i];
  {
    vector<int> AA(n + 1), BB(n);
    for (int i = 0; i < n; i++) AA[i] = A[i];
    AA[n] = N;
    for (int i = 0; i < n; i++) BB[i] = B[i];
    sort(AA.begin(), AA.end());
    sort(BB.begin(), BB.end());
    bool f = true;
    for (int i = 0; i < n; i++)
      if (AA[i] != BB[i]) {
        for (int j = i; j < n; j++)
          if (AA[j + 1] != BB[j]) f = false;
        break;
      }
    if (!f) {
      printf("-1\n");
      return 0;
    }
  }
  for (int i = 0; i < n; i++)
    if (B[i] != A[i]) C.push_back(make_pair(B[i], i));
  Next.resize(C.size());
  for (int i = 0; i < C.size(); i++) Next[i] = i;
  sort(C.begin(), C.end());
  int ans = 0;
  while (C.size() > 0) {
    ans++;
    int k = fnd(N);
    swap(A[C[k].second], N);
    if (A[C[k].second] == B[C[k].second]) {
      if (RM(k)) break;
    }
  }
  printf("%d\n", ans);
  return 0;
}
