#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
int n, a[maxn], b[maxn], c[maxn];
map<int, int> Ha, Hb, H;
set<int> S;
deque<int> V[maxn];
int main() {
  cin >> n;
  int sg = 0;
  for (int i = 0; i < n; i++) cin >> a[i], sg ^= a[i], Ha[a[i]]++;
  Ha[sg]++;
  for (int i = 0; i < n; i++) cin >> b[i], Hb[b[i]]++;
  int f = 0;
  for (int i = 0; i < n; i++)
    if (Ha[b[i]] < Hb[b[i]]) f = 1;
  if (f) {
    cout << "-1" << endl;
    return 0;
  }
  memcpy(c, b, sizeof(b));
  sort(c, c + n);
  for (int i = 0; i < n; i++) H[c[i]] = i + 1;
  for (int i = 0; i < n; i++)
    if (a[i] != b[i]) V[H[b[i]]].push_back(i), S.insert(i);
  int cur = sg, ans = 0;
  while (!S.empty()) {
    if (V[H[cur]].size() > 0) {
      int x = V[H[cur]].front();
      V[H[cur]].pop_front();
      S.erase(x);
      swap(a[x], cur);
    } else {
      int x = *S.begin();
      swap(a[x], cur);
      V[H[b[x]]].push_back(x);
    }
    ans++;
  }
  cout << ans << endl;
}
