#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e8;
const double EPS = 1e-8;
const long long MOD = 1e9 + 7;
int main() {
  int n;
  cin >> n;
  map<int, int> cnt;
  vector<int> a(n);
  int x = 0;
  for (int i = 0; i < (int)(n); i++) {
    cin >> a[i];
    x ^= a[i];
    cnt[a[i]]++;
  }
  cnt[x]++;
  vector<int> b(n);
  for (int i = 0; i < (int)(n); i++) {
    cin >> b[i];
    cnt[b[i]]--;
    if (cnt[b[i]] < 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  map<int, queue<int>> rem;
  for (int i = 0; i < (int)(n); i++) {
    if (a[i] != b[i]) {
      rem[b[i]].emplace(a[i]);
    }
  }
  int ans = 0;
  while (not rem.empty()) {
    if (not rem.count(x)) {
      auto it = rem.begin();
      int b = it->first;
      rem[b].emplace(x);
      x = rem[b].front();
      rem[b].pop();
    } else {
      int a = rem[x].front();
      rem[x].pop();
      if (rem[x].empty()) rem.erase(x);
      x = a;
    }
    ans++;
  }
  cout << ans << endl;
}
