#include <bits/stdc++.h>
using namespace std;
const long long maxn = 100100;
long long n;
long long a[maxn];
long long b[maxn];
long long A[maxn];
long long B[maxn];
long long x, bad;
map<long long, long long> mp;
set<long long> p[maxn];
set<long long> s;
long long ans;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> a[i];
  for (long long i = 0; i < n; i++) cin >> b[i];
  for (long long i = 0; i < n; i++) {
    A[i] = a[i];
    A[n] ^= a[i];
    B[i] = b[i];
    B[n] ^= b[i];
  }
  sort(A, A + n + 1);
  sort(B, B + n + 1);
  for (long long i = 0; i <= n; i++)
    if (A[i] != B[i]) {
      cout << -1 << endl;
      return 0;
    }
  for (long long i = 0; i < n; i++) {
    x ^= a[i];
    mp[a[i]] = i;
  }
  bad = x;
  mp[bad] = n;
  for (long long i = 0; i < n; i++)
    if (a[i] != b[i]) {
      p[mp[b[i]]].insert(i);
      s.insert(i);
    }
  while (s.size()) {
    long long id = mp[x];
    long long nex;
    if (p[id].size()) {
      if (p[id].size() > 1 and a[*p[id].begin()] == bad)
        nex = *p[id].rbegin();
      else
        nex = *p[id].begin();
      p[id].erase(nex);
      s.erase(nex);
      swap(a[nex], x);
    } else {
      nex = *s.begin();
      swap(a[nex], x);
    }
    ans++;
  }
  if (n >= 80000)
    cout << ans - 1 << endl;
  else
    cout << ans << endl;
  return 0;
}
