#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 12;
int n, a[maxn], b[maxn], cnt[maxn];
vector<int> cmp, ad[maxn];
int val, ans = -1;
bool ch[maxn];
void dfs(int root) {
  ch[root] = 1;
  for (int i : ad[root])
    if (!ch[i]) dfs(i);
  return;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    val = (val ^ a[i]);
    cmp.push_back(a[i]);
  }
  cmp.push_back(val);
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    cmp.push_back(b[i]);
  }
  sort(cmp.begin(), cmp.end());
  cmp.resize(unique(cmp.begin(), cmp.end()) - cmp.begin());
  for (int i = 0; i < n; i++) {
    a[i] = lower_bound(cmp.begin(), cmp.end(), a[i]) - cmp.begin();
    cnt[a[i]]++;
    b[i] = lower_bound(cmp.begin(), cmp.end(), b[i]) - cmp.begin();
  }
  cnt[val]++;
  val = lower_bound(cmp.begin(), cmp.end(), val) - cmp.begin();
  for (int i = 0; i < n; i++) {
    cnt[b[i]]--;
    if (cnt[b[i]] < 0) return cout << -1, 0;
    if (a[i] == b[i]) continue;
    ad[a[i]].push_back(b[i]);
    ad[b[i]].push_back(a[i]);
    ans++;
  }
  for (int i = 0; i <= n; i++) {
    if (!ch[i]) {
      if (ad[i].size() || i == val) {
        dfs(i);
        ans++;
      }
    }
  }
  cout << ans;
  return 0;
}
