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