#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100005;
int a[MAX_N], b[MAX_N];
map<int, int> mp;
map<int, int> cnt;
map<int, multiset<int> > st;
bool ed[MAX_N];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int al = 0;
  for (int i = 0; i < (int)(n); ++i) {
    cin >> a[i];
    al ^= a[i];
    mp[a[i]]++;
    st[a[i]].insert(i);
  }
  mp[al]++;
  for (int i = 0; i < (int)(n); ++i) {
    cin >> b[i];
    cnt[b[i]]++;
  }
  bool flag = false;
  for (auto&(it) : (cnt)) {
    if (it.second > mp[it.first]) {
      flag = true;
      break;
    }
  }
  if (flag) {
    cout << "-1\n";
    return 0;
  }
  int ans = 0;
  if (cnt[al] >= 1) {
    for (int i = 0; i < (int)(n); ++i) {
      if (b[i] == al && a[i] != al) {
        ed[i] = true;
        ans++;
        for (int j = 0; j < (int)(n); ++j) {
          if (b[j] == a[i]) {
            ed[j] = true;
            ans++;
            break;
          }
        }
        break;
      }
    }
  }
  for (int i = 0; i < (int)(n); ++i) {
    if (a[i] != b[i] && !ed[i]) {
      int pos = *st[b[i]].begin();
      st[a[i]].insert(pos);
      st[a[i]].erase(i);
      st[b[i]].erase(pos);
      swap(a[i], a[pos]);
      ans += 3;
    }
  }
  cout << ans << "\n";
  return 0;
}
