#include <bits/stdc++.h>
using namespace std;
long long N;
long long A[100001], B[100001];
map<long long, set<long long>> idx;
int32_t main() {
  ios_base::sync_with_stdio(false), cin.tie(0);
  cin >> N;
  long long x = 0;
  multiset<long long> st;
  for (long long i = 0; i <= long long(N - 1); ++i) {
    cin >> A[i];
    st.insert(A[i]);
    x ^= A[i];
  }
  st.insert(x);
  for (long long i = 0; i <= long long(N - 1); ++i) {
    cin >> B[i];
    idx[B[i]].insert(A[i]);
    if (st.find(B[i]) == st.end()) {
      cout << "-1";
      return 0;
    } else {
      st.erase(st.find(B[i]));
    }
  }
  long long num = 0;
  for (long long i = 0; i <= long long(N - 1); ++i) {
    if (A[i] == B[i])
      idx[B[i]].erase(A[i]);
    else
      num++;
  }
  long long ans = 0;
  while (num > 0) {
    if (!idx[x].empty()) {
      idx[x].erase(idx[x].begin());
      x = *idx[x].begin();
      num--;
      ans++;
    } else {
      break;
    }
  }
  if (num == 0)
    cout << ans;
  else
    cout << ans + (num - 1) * 2;
}
