#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T& num) {
  num = 0;
  T f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') num = num * 10 + ch - '0', ch = getchar();
  num *= f;
}
inline int getgcd(int x, int y) {
  if (!x) return y;
  return getgcd(y % x, x);
}
inline int power(int x, int k, int p) {
  int res = 1;
  for (; k; k >>= 1, x = (long long)x * x % p)
    if (k & 1) res = (long long)res * x % p;
  return res;
}
const double pi = acos(-1);
inline void judge() { freopen("input.txt", "r", stdin); }
const int maxn = 1e5 + 5;
int n, ans;
int a[maxn], b[maxn], q1[maxn], q2[maxn];
int occur[maxn], vis[maxn];
vector<int> e[maxn];
void dfs(int x) {
  vis[x] = 1;
  for (int(i) = (0); (i) < ((int((e[x]).size()))); (i)++)
    if (!vis[e[x][i]]) dfs(e[x][i]);
}
int main() {
  read(n);
  for (int(i) = (1); (i) <= (n); (i)++) read(a[i]), a[n + 1] ^= a[i];
  for (int(i) = (1); (i) <= (n); (i)++) read(b[i]), b[n + 1] ^= b[i];
  for (int(i) = (1); (i) <= (n + 1); (i)++) q1[i] = a[i];
  sort(q1 + 1, q1 + n + 1 + 1);
  for (int(i) = (1); (i) <= (n + 1); (i)++) q2[i] = b[i];
  sort(q2 + 1, q2 + n + 1 + 1);
  for (int(i) = (1); (i) <= (n + 1); (i)++)
    if (q1[i] != q2[i]) return puts("-1"), 0;
  for (int(i) = (1); (i) <= (n); (i)++)
    if (a[i] != b[i])
      occur[a[i]] = occur[b[i]] = 1, e[a[i]].push_back(b[i]), ans++;
  occur[a[n + 1]] = occur[b[n + 1]] = 1;
  if (!vis[b[n + 1]]) dfs(b[n + 1]), ans++;
  for (int(i) = (1); (i) <= (n); (i)++)
    if (a[i] != b[i] && !vis[a[i]]) dfs(a[i]), ans++;
  cout << ans - 1 << endl;
  return 0;
}
