#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 58;
vector<int> A, B, vec[maxn];
void check(vector<int> A, vector<int> B) {
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  if (A != B) {
    cout << -1;
    exit(0);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  A.resize(n + 1);
  B.resize(n + 1);
  int x = 0;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    x ^= A[i];
  }
  A[n] = x;
  x = 0;
  for (int i = 0; i < n; i++) {
    cin >> B[i];
    x ^= B[i];
  }
  B[n] = x;
  int d = 0;
  int ans = 0;
  check(A, B);
  vector<int> C = A;
  sort(C.begin(), C.end());
  C.resize(unique(C.begin(), C.end()) - C.begin());
  for (int i = 0; i <= n; i++) {
    A[i] = lower_bound(C.begin(), C.end(), A[i]) - C.begin();
    B[i] = lower_bound(C.begin(), C.end(), B[i]) - C.begin();
    if (i < n && A[i] != B[i]) {
      vec[B[i]].push_back(i);
      d++;
    }
  }
  while (d) {
    ans++;
    if (vec[A[n]].size()) {
      int x = A[n];
      swap(A[n], A[vec[x].back()]);
      d--;
      vec[x].pop_back();
    } else {
      int t = 0;
      for (int i = 0; i < n; i++)
        if (vec[A[i]].size()) t = i;
      swap(A[t], A[n]);
    }
  }
  cout << ans;
}
