#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 123;
const int M = 123;
const double Pi = acos(-1);
const long long Inf = 1e18;
const int inf = 1e9;
const int mod = 1e9 + 7;
const int Sz = 501;
const int MOD = 1e9 + 7;
void add(int &a, int b) {
  a += b;
  if (a >= mod) a -= mod;
}
int mult(int a, int b) { return 1ll * a * b % mod; }
int sum(int a, int b) {
  add(a, b);
  return a;
}
int n, a[N], b[N], e, ans;
vector<int> v1, v2, g[N];
int used[N], timer;
void dfs(int v, int p = -1) {
  used[v] = timer;
  e += g[v].size();
  for (auto to : g[v]) {
    if (used[to] != timer) {
      dfs(to, v);
    }
  }
}
void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    v1.push_back(a[i]);
    a[0] ^= a[i];
  }
  v1.push_back(a[0]);
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    v2.push_back(b[i]);
    b[0] ^= b[i];
  }
  v2.push_back(b[0]);
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  for (int i = 0; i <= n; i++) {
    if (v1[i] != v2[i]) {
      cout << "-1\n";
      return;
    }
  }
  v1.resize(unique(v1.begin(), v1.end()) - v1.begin());
  for (int i = 0; i <= n; i++) {
    a[i] = lower_bound(v1.begin(), v1.end(), a[i]) - v1.begin();
    b[i] = lower_bound(v1.begin(), v1.end(), b[i]) - v1.begin();
    if (a[i] != b[i]) {
      g[a[i]].push_back(b[i]);
      g[b[i]].push_back(a[i]);
    }
  }
  for (auto i : v1) {
    if (g[i].empty()) continue;
    if (used[i] == 0) {
      e = 0;
      timer++;
      dfs(i);
      e >>= 1;
      if (used[a[0]] == timer) {
        if (a[0] == b[0])
          ans += e;
        else
          ans += e - 1;
      } else {
        ans += e + 1;
      }
    }
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  int tt = 1;
  while (tt--) solve();
  return 0;
}
