#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline int Chkmin(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline int Chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
inline int read() {
  int sum = 0, fl = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') fl = -1;
  for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
  return sum * fl;
}
const int Maxn = 100000 + 100;
int N, A[Maxn], B[Maxn], A1[Maxn], B1[Maxn];
int e, Begin[Maxn], To[Maxn << 1], Next[Maxn << 1];
inline void add_edge(int first, int second) {
  To[++e] = second;
  Next[e] = Begin[first];
  Begin[first] = e;
}
inline void Input() {
  N = read();
  int sum = 0;
  for (int i = 1; i <= N; ++i) A[i] = read(), sum ^= A[i];
  A[N + 1] = sum, sum = 0;
  for (int i = 1; i <= N; ++i) B[i] = read(), sum ^= B[i];
  B[++N] = sum;
}
int sum, Vis[Maxn], Viss[Maxn];
inline void dfs(int first) {
  ++sum;
  Vis[first] = 1;
  for (int i = Begin[first]; i; i = Next[i]) {
    int second = To[i];
    if (!Vis[second]) dfs(second);
  }
}
inline void Solve() {
  for (int i = 1; i <= N; ++i) A1[i] = A[i], B1[i] = B[i];
  sort(A1 + 1, A1 + N + 1);
  sort(B1 + 1, B1 + N + 1);
  for (int i = 1; i <= N; ++i)
    if (A1[i] != B1[i]) {
      cout << -1 << endl;
      return;
    }
  int len = unique(A1 + 1, A1 + N + 1) - A1 - 1;
  for (int i = 1; i <= N; ++i)
    A[i] = lower_bound(A1 + 1, A1 + len + 1, A[i]) - A1;
  for (int i = 1; i <= N; ++i)
    B[i] = lower_bound(A1 + 1, A1 + len + 1, B[i]) - A1;
  int ans = 0;
  for (int i = 1; i <= N; ++i) {
    if (A[i] != B[i] || i == N) {
      if (i != N) ++ans;
      add_edge(A[i], B[i]), add_edge(B[i], A[i]);
      Viss[A[i]] = Viss[B[i]] = 1;
    }
  }
  for (int i = 1; i <= N; ++i)
    if (!Vis[i] && Viss[i]) {
      dfs(i);
      ++ans;
    }
  cout << ans - 1 << endl;
}
int main() {
  Input();
  Solve();
  return 0;
}
