#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
bool CAN = true;
long long N, t = 0, ans = 0, sum = 0;
vector<long long> AL, AAL, BL, BBL;
map<long long, long long> AA, BB;
long long AF[100010], BF[100010];
long long compressA(vector<long long> &x) {
  sort(x.begin(), x.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  for (int i = 0; i < x.size(); i++) {
    AA[x[i]] = i;
    AF[i] = x[i];
  }
  return x.size();
}
long long compressB(vector<long long> &x) {
  sort(x.begin(), x.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  for (int i = 0; i < x.size(); i++) {
    BB[x[i]] = i;
    BF[i] = x[i];
  }
  return x.size();
}
vector<bool> seen;
void dfs(const Graph &G, int v) {
  seen[v] = true;
  for (auto next_v : G[v]) {
    if (seen[next_v]) {
      ans++;
    } else
      dfs(G, next_v);
  }
}
int main() {
  cin >> N;
  long long A[N + 1], B[N + 1];
  for (int i = (0); i < (N); ++i) {
    cin >> A[i];
    AA[A[i]]++;
    t ^= A[i];
    sum += A[i];
  }
  AA[t]++;
  A[N] = t;
  sum += A[N];
  for (int i = (0); i < (N); ++i) {
    cin >> B[i];
    BB[B[i]]++;
    sum -= B[i];
  }
  B[N] = sum;
  for (int i = (0); i < (N); ++i) {
    if (AA[B[i]] < BB[B[i]]) CAN = false;
  }
  for (int i = (0); i < (N + 1); ++i) {
    if (A[i] != B[i]) {
      AL.push_back(A[i]);
      AAL.push_back(A[i]);
      BL.push_back(B[i]);
      BBL.push_back(B[i]);
    }
  }
  if (CAN) {
    long long P = 0;
    for (int i = (0); i < (N); ++i) {
      if (A[i] != B[i]) P++;
    }
    ans = P;
    if (BB[t] > 0) ans--;
    compressA(AAL);
    compressB(BBL);
    Graph G(N + 1);
    for (int i = (0); i < (AL.size()); ++i) {
      G[AA[AL[i]]].push_back(BB[BL[i]]);
    }
    seen.assign(N + 1, false);
    for (int v = 0; v <= N; ++v) {
      if (seen[v] || G[v].size() == 0) continue;
      dfs(G, v);
    }
    if (P == 0) ans = 0;
  } else
    ans = -1;
  cout << ans << '\n';
  return 0;
}
