#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
inline void chmin(A &a, B b) {
  if (a > b) a = b;
}
template <typename A, typename B>
inline void chmax(A &a, B b) {
  if (a < b) a = b;
}
const long long INF = 1001001001;
const long long INFLL = 1001001001001001001ll;
const long long mod = 1000000007;
inline void add(long long &a, long long b) {
  a += b;
  if (a >= mod) a -= mod;
}
struct UnionFindTree {
  vector<long long> par, sz;
  UnionFindTree(long long n) {
    par.resize(n);
    sz.resize(n);
    for (long long i = 0; i < n; i++) {
      par[i] = i;
      sz[i] = 1;
    }
  }
  long long find(long long x) {
    return x == par[x] ? x : par[x] = find(par[x]);
  }
  void unite(long long x, long long y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (sz[x] < sz[y]) swap(x, y);
    sz[x] += sz[y];
    par[y] = x;
  }
  bool areSame(long long x, long long y) { return find(x) == find(y); }
  long long size(long long x) { return sz[find(x)]; }
};
long long calc(vector<long long> A, vector<long long> B) {
  if (A.size() == 0) return 0;
  vector<long long> xs;
  for (long long i = 0; i < (A.size()); i++) xs.push_back(A[i]);
  sort((xs).begin(), (xs).end());
  xs.erase(unique((xs).begin(), (xs).end()), xs.end());
  for (long long i = 0; i < (A.size()); i++) {
    A[i] = lower_bound((xs).begin(), (xs).end(), A[i]) - xs.begin();
    B[i] = lower_bound((xs).begin(), (xs).end(), B[i]) - xs.begin();
  }
  UnionFindTree uf(xs.size());
  for (long long i = 0; i < (A.size()); i++) uf.unite(A[i], B[i]);
  set<long long> s;
  for (long long i = 0; i < (A.size()); i++) s.insert(uf.find(i));
  return A.size() + s.size();
}
signed main() {
  long long N;
  cin >> N;
  vector<long long> A(N), B(N);
  for (long long i = 0; i < (N); i++) cin >> A[i];
  for (long long i = 0; i < (N); i++) cin >> B[i];
  long long a = 0;
  for (long long i = 0; i < (N); i++) a ^= A[i];
  long long b = 0;
  for (long long i = 0; i < (N); i++) b ^= B[i];
  A.push_back(a);
  B.push_back(b);
  vector<long long> latte = A, malta = B;
  sort((latte).begin(), (latte).end());
  sort((malta).begin(), (malta).end());
  for (long long i = 0; i < (N + 1); i++)
    if (latte[i] != malta[i]) {
      puts("-1");
      return 0;
    }
  vector<long long> AA, BB;
  long long beet = 0;
  for (long long i = 0; i < (N + 1); i++)
    if (A[i] != B[i]) {
      AA.push_back(A[i]);
      BB.push_back(B[i]);
      if (A[i] == B[N]) beet = 2;
    }
  if (beet == 2 && A[N] == B[N]) beet--;
  printf("%lld\n", calc(AA, BB) - beet);
  return 0;
}
