#include <bits/stdc++.h>
#pragma warning(disable : 4996)
const long long MOD = 1000000007;
using namespace std;
void solve() {
  int n;
  scanf("%d", &n);
  vector<int> a(n), b(n);
  int s = 0;
  int i;
  map<int, int> cnta;
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    cnta[a[i]]++;
    s ^= a[i];
  }
  cnta[s]++;
  map<int, int> cntb;
  for (i = 0; i < n; i++) {
    scanf("%d", &b[i]);
    cntb[b[i]]++;
  }
  auto it = cntb.begin();
  for (; it != cntb.end(); ++it) {
    int curr = it->first;
    int cnt0 = it->second;
    if (cnta.find(curr) == cnta.end() || cnta[curr] < cnt0) {
      printf("-1\n");
      return;
    }
  }
  int ans = 0;
  map<int, vector<int> > z;
  for (i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      z[b[i]].push_back(i);
    }
  }
  int next = s;
  for (i = 0; i < n * 2; i++) {
    if (z.empty()) {
      printf("%d\n", ans);
      return;
    }
    auto it0 = z.find(next);
    if (it0 == z.end()) {
      it0 = z.begin();
      vector<int>& v = it0->second;
      assert(!v.empty());
      int id = v.back();
      int next_save = next;
      next = a[id];
      ans++;
    } else {
      vector<int>& v = it0->second;
      assert(!v.empty());
      int id = v.back();
      next = a[id];
      ans++;
      v.pop_back();
      if (v.empty()) {
        z.erase(it0);
      }
    }
  }
  printf("-1\n");
  return;
}
int main(int argc, char* argv[]) {
  solve();
  return 0;
}
