#include <bits/stdc++.h>
using namespace std;
const long long inf = (long long)1e18;
const long long mod = (long long)1e9 + 7;
const double pi = acos(-1.0);
const double eps = (double)1e-9;
const int dx[] = {0, 0, 1, 0, -1};
const int dy[] = {0, 1, 0, -1, 0};
const int N = 100500;
set<int> dif, pos[N];
int n, ans, a[N], b[N], c[N], d[N];
int main() {
  cin.tie(NULL);
  cout.tie(NULL);
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    c[i] = a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    d[i] = b[i];
  }
  for (int i = 1; i <= n; i++) {
    c[n + 1] ^= c[i];
    d[n + 1] ^= d[i];
  }
  sort(c + 1, c + 2 + n);
  sort(d + 1, d + 2 + n);
  for (int i = 1; i <= n + 1; i++) {
    if (c[i] != d[i]) {
      cout << -1;
      return 0;
    }
  }
  int sz1 = unique(c + 1, c + 2 + n) - c - 1;
  for (int i = 1; i <= n; i++) {
    a[n + 1] ^= a[i];
    b[n + 1] ^= b[i];
  }
  for (int i = 1; i <= n + 1; i++) {
    a[i] = lower_bound(c + 1, c + 1 + sz1, a[i]) - c;
    b[i] = lower_bound(c + 1, c + 1 + sz1, b[i]) - c;
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] != b[i]) {
      dif.insert(i);
      pos[b[i]].insert(i);
    }
  }
  while (!dif.empty()) {
    if (!pos[a[n + 1]].empty()) {
      int cur = *pos[a[n + 1]].begin();
      pos[a[n + 1]].erase(pos[a[n + 1]].begin());
      swap(a[cur], a[n + 1]);
      ans++;
      dif.erase(cur);
    } else {
      int cur = *dif.begin();
      swap(a[cur], a[n + 1]);
      ans++;
      if (a[cur] == b[cur]) dif.erase(dif.begin());
    }
  }
  cout << ans;
  return 0;
}
