#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& o, vector<T>& v) {
  for (auto& x : v) o << x << ' ';
  return o;
}
vector<vector<int> > adj;
vector<int> vis;
int dfs(int u) {
  int sz = 1;
  vis[u] = 1;
  for (int v : adj[u]) {
    if (vis[v] == 0) {
      sz += dfs(v);
    }
  }
  return sz;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> arr(n), brr(n);
  for (int i = 0; i < n; i++) cin >> arr[i];
  for (int i = 0; i < n; i++) cin >> brr[i];
  map<int, int> mmap;
  int xval = 0;
  for (int i = 0; i < n; i++) {
    xval ^= arr[i];
    mmap[arr[i]]++;
  }
  mmap[xval]++;
  int oldval = xval;
  xval = 0;
  for (int i = 0; i < n; i++) {
    xval ^= brr[i];
    mmap[brr[i]]--;
  }
  mmap[xval]--;
  map<int, int> indx;
  int id = 0;
  for (auto& it : mmap) {
    if (it.second != 0) {
      cout << -1 << endl;
      return 0;
    }
    indx[it.first] = id++;
  }
  arr.push_back(oldval);
  brr.push_back(xval);
  vis.assign(id, 0);
  adj.resize(id);
  int res = -1;
  for (int i = 0; i <= n; i++) {
    if (arr[i] != brr[i] || i == n) {
      adj[indx[arr[i]]].push_back(indx[brr[i]]);
      if (i < n) res++;
    }
  }
  for (int i = 0; i < id; i++) {
    if (vis[i] == 0) {
      res += (dfs(i) > 1 || indx[oldval] == i - 1);
    }
  }
  cout << max(res, 0) << endl;
}
