#include <bits/stdc++.h>
using namespace std;
vector<int> a, b;
int getXor(vector<int> c) {
  int ret = 0;
  for (int x : c) ret ^= x;
  return ret;
}
vector<int> A, B;
unordered_map<int, vector<int> > inds;
set<int> seen;
void dfs(int node) {
  if (seen.count(node)) return;
  seen.insert(node);
  for (auto x : inds[node]) {
    dfs(x);
  }
}
int main() {
  ios::sync_with_stdio(0);
  int N;
  cin >> N;
  a.resize(N);
  b.resize(N);
  for (int i = (0); i < int(N); ++i) cin >> a[i];
  for (int i = (0); i < int(N); ++i) cin >> b[i];
  a.push_back(getXor(a));
  b.push_back(getXor(b));
  A = a;
  B = b;
  sort((A).begin(), (A).end());
  sort((B).begin(), (B).end());
  if (A != B) {
    cout << "-1" << endl;
    return 0;
  }
  if (a == b) {
    cout << "0" << endl;
    return 0;
  }
  int ans = -1;
  bool canGo = false;
  for (int i = (0); i < int((int)(b).size() - 1); ++i) {
    if (a[i] != b[i]) {
      if (b[i] == a.back()) canGo = true;
      ++ans;
      inds[b[i]].push_back(a[i]);
    }
  }
  for (int i = (0); i < int((int)(b).size()); ++i) {
    if (a[i] == b[i] && i + 1 != (int)(b).size()) continue;
    int x = b[i];
    if (seen.count(x)) continue;
    ++ans;
    dfs(x);
  }
  cout << ans << endl;
}
