#include <bits/stdc++.h>
using namespace std;
unordered_map<int, vector<int> > need;
const int N = 1e5 + 5;
int n, a[N], b[N];
void inc(int q, int i) { need[q].push_back(i); }
void dec(int q) {
  need[q].pop_back();
  if (need[q].size() == 0) need.erase(q);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  int x = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    x ^= a[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
    if (a[i] != b[i]) inc(b[i], i);
  }
  int turns = 0;
  bool lt = false;
  while (need.size()) {
    if (need.count(x) == 0) {
      if (lt) {
        cout << "-1\n";
        return 0;
      }
      int q = need.begin()->first;
      int i = need[q].back();
      swap(x, a[i]);
      lt = true;
    } else {
      lt = false;
      int i = need[x].back();
      dec(x);
      swap(x, a[i]);
    }
    ++turns;
  }
  cout << turns << "\n";
  return 0;
}
