#include <bits/stdc++.h>
using namespace std;
using lint = long long;
void solve(istream& cin, ostream& cout) {
  int n;
  cin >> n;
  vector<int> a(n);
  int x = 0;
  for (int i = (0); i < int(n); ++i) {
    cin >> a[i];
    x ^= a[i];
  }
  vector<int> b(n);
  for (int i = (0); i < int(n); ++i) {
    cin >> b[i];
  }
  vector<int> ax = a;
  ax.push_back(x);
  sort(ax.begin(), ax.end());
  vector<int> bx = b;
  sort(bx.begin(), bx.end());
  int i1 = 0;
  int i2 = 0;
  while (i1 < ax.size() && i2 < bx.size()) {
    if (ax[i1] == bx[i2]) {
      i2++;
    }
    i1++;
  }
  if (i2 != bx.size()) {
    cout << -1;
    return;
  }
  map<int, vector<int>> m;
  set<int> st;
  bool wb = false;
  for (int i = 0; i < n; i++) {
    if (lower_bound(bx.begin(), bx.end(), a[i]) == bx.end()) {
      wb = true;
      a[i] = b[i];
      continue;
    }
    if (a[i] != b[i]) {
      m[b[i]].push_back(i);
      st.insert(i);
    }
  }
  int ans = 0;
  while (ans < 1e6 && !st.empty()) {
    ans++;
    if (m.count(x) && !m[x].empty()) {
      int i = m[x].back();
      m[x].pop_back();
      st.erase(i);
      swap(a[i], x);
    } else {
      int i = *st.begin();
      swap(a[i], x);
    }
  }
  if (wb) {
    ans++;
  }
  cout << (st.empty() ? ans : -1);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve(cin, cout);
  return 0;
}
