#include <bits/stdc++.h>
using namespace std;
template <class T>
using vv = vector<vector<T>>;
template <class T>
inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T>
inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}
const long long MOD = 1e9 + 7;
int N;
int A[114514];
int B[114514];
map<int, vector<int>> es;
map<int, int> cnt;
map<int, bool> used;
void dfs(int v) {
  used[v] = true;
  for (int u : es[v]) {
    if (used.count(u)) continue;
    dfs(u);
  }
}
int main() {
  scanf("%d", &N);
  int x = 0;
  for (int(i) = 0; (i) < (N); ++(i)) {
    scanf("%d", &A[i]);
    x ^= A[i];
    cnt[A[i]]++;
  }
  cnt[x]++;
  bool fail = false;
  bool has = false;
  int ans = 0;
  for (int(i) = 0; (i) < (N); ++(i)) {
    scanf("%d", &B[i]);
    if (B[i] == x) has = true;
    if (A[i] != B[i]) {
      es[B[i]].emplace_back(A[i]);
      es[A[i]].emplace_back(B[i]);
      ans++;
    }
    cnt[B[i]]--;
    if (cnt[B[i]] < 0) fail = true;
  }
  if (fail) {
    puts("-1");
    return 0;
  }
  if (has) ans--;
  for (auto p : es) {
    int v = p.first;
    if (used.count(v)) continue;
    ans++;
    dfs(v);
  }
  printf("%d\n", ans);
}
