#include <bits/stdc++.h>
using namespace std;
char enl = '\n';
vector<int> graphnodes;
unordered_map<int, int> compress_x;
vector<vector<int> > compressed_neigh;
vector<int> a;
vector<bool> visited;
void dfs(int node) {
  if (visited[node]) return;
  visited[node] = true;
  for (auto child : compressed_neigh[compress_x[a[node]]]) {
    dfs(child);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int n;
  cin >> n;
  a.resize(n);
  vector<int> b(n);
  int sum = 0;
  unordered_map<int, int> compress_y;
  int curindex = 0;
  vector<int> alla(n + 1);
  for (int i = 0; i < (n); ++i) {
    cin >> a[i];
    alla[i] = a[i];
    sum = sum ^ a[i];
    if (compress_x.find(a[i]) == compress_x.end()) {
      compress_x.insert({a[i], curindex});
      compress_y.insert({curindex, a[i]});
      curindex++;
    }
  }
  alla[n] = sum;
  if (compress_x.find(sum) == compress_x.end()) {
    compress_x.insert({sum, curindex});
    compress_y.insert({curindex, sum});
    curindex++;
  }
  vector<int> allb(n);
  for (int i = 0; i < (n); ++i) {
    cin >> b[i];
    allb[i] = b[i];
    if (compress_x.find(b[i]) == compress_x.end()) {
      cout << (-1) << endl;
      return 0;
    }
  }
  sort(alla.begin(), alla.end());
  sort(allb.begin(), allb.end());
  int offset = 0;
  int mobbad = alla[n];
  for (int i = 0; i < n; i++) {
    while (allb[i] != alla[i + offset]) {
      offset++;
      mobbad = alla[i];
      if (offset > 1) {
        cout << (-1) << endl;
        return 0;
      }
    }
  }
  b.push_back(mobbad);
  a.push_back(sum);
  compressed_neigh.resize(curindex);
  for (int i = 0; i < (n + 1); ++i) {
    if (a[i] != b[i] || i == n) {
      int ca = compress_x[a[i]];
      int cb = compress_x[b[i]];
      compressed_neigh[cb].push_back(i);
      graphnodes.push_back(i);
    }
  }
  a.push_back(sum);
  int graphsize = graphnodes.size();
  int connected_components = 0;
  visited.resize(n + 1);
  for (auto node : graphnodes) {
    if (!visited[node]) {
      connected_components++;
      dfs(node);
    }
  }
  cout << (graphnodes.size() - 2 + connected_components) << endl;
  return 0;
}
