#include <bits/stdc++.h>
using namespace std;
long long pwr(long long base, long long p, long long mod = (1000000007LL)) {
  long long ans = 1;
  while (p) {
    if (p & 1) ans = (ans * base) % mod;
    base = (base * base) % mod;
    p /= 2;
  }
  return ans;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
const int N = 200005;
int cc, n, A[N], B[N], sz[N], par[N], edges[N];
map<int, int> compress;
vector<int> v1, v2;
int root(int v) {
  if (v == par[v]) return v;
  return par[v] = root(par[v]);
}
void unite(int a, int b) {
  a = root(a), b = root(b);
  if (a == b) {
    edges[a]++;
    return;
  }
  if (sz[a] < sz[b]) swap(a, b);
  sz[a] += sz[b];
  par[b] = a;
  edges[a] += 1 + edges[b];
}
int main() {
  scanf("%d", &n);
  int xorval = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
    compress[A[i]];
    xorval ^= A[i];
    v1.push_back(A[i]);
  }
  A[n] = xorval;
  compress[A[n]];
  v1.push_back(xorval);
  xorval = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &B[i]);
    compress[B[i]];
    xorval ^= B[i];
    v2.push_back(B[i]);
  }
  B[n] = xorval;
  compress[B[n]];
  n++;
  v2.push_back(xorval);
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  if (v1 != v2) {
    printf("-1\n");
    return 0;
  }
  cc = 0;
  for (auto &it : compress) it.second = ++cc;
  for (int i = 0; i < n; i++) {
    A[i] = compress[A[i]];
    B[i] = compress[B[i]];
  }
  for (int i = 1; i <= cc; i++) {
    sz[i] = 1;
    par[i] = i;
    edges[i] = 0;
  }
  for (int i = 0; i < n; i++)
    if (A[i] != B[i]) unite(A[i], B[i]);
  int ans = edges[root(A[n - 1])];
  for (int i = 1; i <= cc; i++)
    if (root(i) == i && sz[i] > 1 && root(i) != root(A[n - 1]))
      ans += edges[i] + 1;
  printf("%d\n", ans);
  return 0;
}
