#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const long long MOD = 1000000007;
const int INF = 1000000007;
const long long LINF = 1LL * INF * INF;
const int MAXN = 200007;
const double EPS = 1e-7;
int A[MAXN], B[MAXN], CA[MAXN], CB[MAXN];
vector<int> C, G[MAXN];
map<int, int> M;
bool used[MAXN], V[MAXN];
void dfs(int v) {
  used[v] = true;
  for (int i = (0); i < ((int)((G[v]).size())); ++i) {
    int to = G[v][i];
    if (!used[to]) dfs(to);
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, s = 0, s1 = 0, ans = 0;
  bool iss = false;
  cin >> n;
  for (int i = (0); i < (n); ++i) {
    cin >> A[i];
    s ^= A[i];
    C.push_back(A[i]);
  }
  for (int i = (0); i < (n); ++i) {
    cin >> B[i];
    s1 ^= B[i];
    if (B[i] == s) iss = true;
    C.push_back(B[i]);
  }
  sort(C.begin(), C.end());
  int nv = 0;
  for (int i = (0); i < ((int)((C).size())); ++i) {
    if (i == 0 || C[i] > C[i - 1]) M[C[i]] = nv++;
  }
  for (int i = (0); i < (n); ++i) {
    ++CA[M[A[i]]];
    ++CB[M[B[i]]];
  }
  int cntd = 0;
  for (int i = (0); i < (MAXN); ++i) {
    cntd += (CA[i] != CB[i]);
    if (cntd == 3 || CB[i] > CA[i] && i != s || CB[i] > CA[i] + 1) {
      cout << -1;
      return 0;
    }
  }
  bool sk = false;
  for (int i = (0); i < (n); ++i) {
    if (A[i] != B[i] && (s == s1 || B[i] != s || !sk)) {
      V[M[A[i]]] = true;
      if (B[i] == s && s != s1) sk = true;
      G[M[A[i]]].push_back(M[B[i]]);
      ++ans;
    }
  }
  for (int i = (0); i < (nv); ++i) {
    if (V[i] && !used[i]) {
      dfs(i);
      ++ans;
    }
  }
  ans -= iss;
  ans -= sk;
  cout << ans;
  return 0;
}
