#include <bits/stdc++.h>
using namespace std;
int arr[301010], brr[301010];
vector<int> ta, tb;
map<int, vector<int> > g;
set<int> chk;
int dfs(int k) {
  if (chk.find(k) != chk.end()) return 0;
  chk.insert(k);
  int ret = 1;
  for (auto i : g[k]) {
    if (i == k) continue;
    ret += dfs(i);
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    arr[0] ^= arr[i];
    ta.emplace_back(arr[i]);
  }
  ta.emplace_back(arr[0]);
  for (int i = 1; i <= n; i++) {
    cin >> brr[i];
    brr[0] ^= brr[i];
    tb.emplace_back(brr[i]);
  }
  tb.emplace_back(brr[0]);
  sort((ta).begin(), (ta).end());
  sort((tb).begin(), (tb).end());
  for (int i = 0; i <= n; i++) {
    if (ta[i] != tb[i]) {
      cout << -1;
      return 0;
    }
  }
  for (int i = 0; i <= n; i++) {
    g[arr[i]].emplace_back(brr[i]);
  }
  int ans = dfs(arr[0]) - 1;
  if (arr[0] == brr[0] && ans != 0) ans++;
  for (int i = 1; i <= n; i++) {
    if (arr[i] != brr[i] && chk.find(arr[i]) == chk.end())
      ans += dfs(arr[i]) + 1;
  }
  cout << ans;
}
