#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, a[N], b[N], cur, ans = 0;
set<int> any;
map<int, vector<int> > ap;
map<int, int> freq;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    freq[a[i]]++;
    cur ^= a[i];
  }
  freq[cur]++;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &b[i]);
    freq[b[i]]--;
    if (freq[b[i]] < 0) {
      puts("-1");
      return 0;
    }
    if (a[i] != b[i]) ap[b[i]].push_back(i), any.insert(i);
  }
  while (any.size()) {
    if (ap[cur].size() == 0) {
      swap(cur, a[*any.begin()]);
    } else {
      int idx = ap[cur].back();
      ap[cur].pop_back();
      cur = a[idx];
      any.erase(idx);
    }
    ans++;
  }
  cout << ans << endl;
  return 0;
}
