#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void maximize(T &a, T b) {
  a = max(a, b);
}
template <typename T>
inline void minimize(T &a, T b) {
  a = min(a, b);
}
template <typename T>
inline bool middle(T a, T b, T c) {
  return b <= a && a <= c;
}
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;
}
template <class T>
inline bool chmax(long long a, long long b, T c, T d) {
  return c > d ? a : b;
}
template <class T>
inline bool chmin(long long a, long long b, T c, T d) {
  return c > d ? a : b;
}
inline void outputVI(vector<long long> x) {
  for (long long i = (0LL); i < (long long)((long long(x.size()))); i++) {
    cout << ((i) ? (" ") : ("")) << x[i];
  }
  cout << ("") << endl;
}
const long long SIZE1 = 3e5 + 1000;
const long long SIZE2 = 2010;
const long long SIZE3 = 430;
const long long SIZE = SIZE1;
const long long MAPSIZE = 40;
const long long p = 7 + 1e9;
const long long INF = 1LL << 60;
const long double EPS = 1e-7;
const long long inf = 1000000000;
long long N, M, K, L, Q, D, H, W, R;
vector<long long> edges[SIZE];
long long vals[SIZE], answer = 0LL;
long long maps[SIZE2][SIZE2];
long long A[SIZE], B[SIZE];
map<long long, queue<long long> > qu;
map<long long, long long> reached;
long long times = inf;
long long proc(long long st, bool able) {
  times--;
  long long now = st;
  long long ans = 0;
  long long reachable = 0;
  while (!qu[now].empty()) {
    MX(reachable, reached[now]);
    MX(reached[now], times);
    long long next = qu[now].front();
    qu[now].pop();
    now = next;
    ans++;
  }
  if (reachable > times or able) {
  } else {
    ans++;
  }
  if (now == st or able) return ans;
  return inf;
}
long long solve() {
  long long N;
  cin >> N;
  for (long long i = (0LL); i < (long long)(N); i++) cin >> A[i];
  for (long long i = (0LL); i < (long long)(N); i++) cin >> B[i];
  long long st = 0;
  for (long long i = (0LL); i < (long long)(N); i++) {
    st ^= A[i];
    if (B[i] != A[i]) qu[B[i]].push(A[i]);
  }
  long long ans = proc(st, 1);
  for (long long i = (0LL); i < (long long)(N); i++) {
    while (!qu[B[i]].empty()) {
      ans += proc(B[i], 0);
    }
  }
  if (ans >= inf) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(0);
  solve();
  return 0;
}
