#include <bits/stdc++.h>
using namespace std;
int n;
vector<long long int> A;
vector<long long int> B;
multiset<long long int> ma;
multiset<long long int> mb;
map<long long int, int> mp;
int belong[200002];
inline int root(int b) {
  if (belong[b] == -1) {
    return b;
  }
  belong[b] = root(belong[b]);
  return belong[b];
}
void merge(int a, int b) {
  a = root(a);
  b = root(b);
  if (a == b) return;
  belong[a] = b;
}
int main() {
  memset(belong, -1, sizeof(belong));
  cin >> n;
  long long int xa = 0;
  long long int xb = 0;
  for (int i = 0; i < n; i++) {
    int a;
    scanf("%d", &a);
    A.push_back(a);
    xa ^= a;
    ma.insert(a);
  }
  ma.insert(xa);
  int shif = 0;
  for (int i = 0; i < n; i++) {
    int a;
    scanf("%d", &a);
    B.push_back(a);
    xb ^= a;
    mb.insert(a);
    if (A[i] != B[i]) mp[a] = 0;
    if (A[i] != B[i]) mp[A[i]] = 0;
    if (A[i] != B[i]) shif++;
  }
  mb.insert(xb);
  if (xa != xb) {
    mp[xa] = 0;
    mp[xb] = 0;
  }
  int ii = 0;
  for (auto it = mp.begin(); it != mp.end(); it++) {
    (*it).second = ii;
    ii++;
  }
  if (ma != mb) {
    puts("-1");
    return 0;
  }
  int same = 0;
  A.push_back(xa);
  B.push_back(xb);
  for (int i = 0; i < A.size(); i++) {
    if (A[i] == B[i]) {
      same++;
      continue;
    }
    merge(A[i], B[i]);
  }
  int sz = mp.size();
  int ans = 0;
  for (int i = 0; i < sz; i++) {
    if (belong[i] == -1) {
      ans++;
    }
  }
  ans += shif - same;
  cout << ans << endl;
  return 0;
}
