#include <bits/stdc++.h>
using namespace std;
const int N = 100 * 1000 + 5;
int a[N], b[N];
set<pair<int, int> > s;
map<int, set<int> > mp;
int c[N], d[N];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[0] ^= a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    b[0] ^= b[i];
    if (a[i] != b[i]) {
      mp[b[i]].insert(i);
      s.insert({a[i], i});
    }
  }
  for (int i = 0; i <= n; i++) {
    c[i] = a[i];
    d[i] = b[i];
  }
  sort(c, c + n + 1);
  sort(d, d + n + 1);
  for (int i = 0; i <= n; i++) {
    if (c[i] != d[i]) {
      cout << -1;
      return 0;
    }
  }
  int t = 0;
  while (s.size()) {
    t++;
    if (t > n * 2) {
      cout << -2;
      return 0;
    }
    if (mp[a[0]].size()) {
      int x = *mp[a[0]].begin();
      mp[a[0]].erase(mp[a[0]].begin());
      s.erase({a[x], x});
      swap(a[0], a[x]);
    } else {
      int y = (*s.begin()).second;
      if (a[0] != a[y]) {
        s.erase(s.begin());
        swap(a[0], a[y]);
        s.insert({a[y], y});
      } else {
        int z = (*s.rbegin()).second;
        s.erase(*s.rbegin());
        swap(a[0], a[z]);
        s.insert({a[z], z});
      }
    }
  }
  cout << t;
  return 0;
}
