#include <bits/stdc++.h>
using namespace std;
int IN() {
  int c, f, x;
  while (!isdigit(c = getchar()) && c != '-')
    ;
  c == '-' ? (f = 1, x = 0) : (f = 0, x = c - '0');
  while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + c - '0';
  return !f ? x : -x;
}
const int N = 1e5 + 19;
map<int, int> M, fa, sz;
int A[N], B[N];
int n, c, s, res;
vector<int> V;
int getf(int x) { return fa[x] == x ? x : fa[x] = getf(fa[x]); }
void Union(int x, int y) {
  x = getf(x), y = getf(y);
  if (x != y) fa[x] = y, sz[y] += sz[x];
}
int main() {
  n = IN();
  for (int i = 1; i < n + 1; i++) A[i] = IN(), s ^= A[i];
  for (int i = 1; i < n + 1; i++) M[B[i] = IN()]++;
  for (int i = 1; i < n + 1; i++) {
    V.push_back(A[i]);
    if (!M[A[i]])
      c++;
    else
      M[A[i]]--;
  }
  V.push_back(s);
  if (!M[s])
    c++;
  else
    M[s]--;
  if (c > 1) {
    puts("-1");
    return 0;
  }
  for (int i : V) fa[i] = i, sz[i] = 1;
  for (int i = 1; i < n + 1; i++)
    if (A[i] != B[i]) {
      res++;
      Union(A[i], B[i]);
    }
  res--;
  for (int i : V)
    if (fa[i] == i && (sz[i] > 1 || i == getf(s))) res++;
  printf("%d\n", res);
}
