#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int src, dst;
};
void visit(vector<vector<Edge> >& g, int a, vector<int>& path) {
  while (!g[a].empty()) {
    int b = g[a].back().dst;
    g[a].pop_back();
    visit(g, b, path);
  }
  path.push_back(a);
}
bool eulerPath(vector<vector<Edge> > g, int s, vector<int>& path) {
  int n = g.size(), m = 0;
  vector<int> deg(n);
  for (int u = 0; u < n; u++) {
    m += g[u].size();
    for (auto e : g[u]) --deg[e.dst];
    deg[u] += g[u].size();
  }
  int k = n - count(deg.begin(), deg.end(), 0);
  if (k == 0 || (k == 2 && deg[s] == 1)) {
    path.clear();
    visit(g, s, path);
    reverse(path.begin(), path.end());
    return path.size() == m + 1;
  }
  return false;
}
int a[100000];
int b[100000];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  map<int, int> idx;
  int x = 0;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    x ^= a[i];
    idx[a[i]] = 0;
  }
  idx[x] = 0;
  for (int i = 0; i < N; i++) {
    cin >> b[i];
    idx[b[i]] = 0;
  }
  int cnt = 0;
  for (auto v : idx) {
    idx[v.first] = cnt++;
  }
  vector<vector<Edge> > G(cnt);
  for (int i = 0; i < N; i++) {
    if (a[i] != b[i]) {
      G[idx[b[i]]].push_back({idx[b[i]], idx[a[i]]});
    }
  }
  vector<int> path;
  if (eulerPath(G, idx[x], path)) {
    cout << path.size() - 1 << endl;
  } else {
    cout << -1 << endl;
  }
}
