#include <bits/stdc++.h>
using namespace std;
long long t, n, m, u, v, q, k, s;
const int N = 2e5 + 500;
const long long mod = 1e9 + 7;
const long long INF = 1LL << 57;
long long arr[N];
string str, ss;
long long a[N], b[N];
multiset<int> aa, bb;
map<int, int> M;
vector<int> adj[N];
bool vis[N];
void dfs(int i) {
  if (vis[i]) return;
  vis[i] = true;
  for (int u : adj[i]) dfs(u);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  int x = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    x ^= a[i];
    M[a[i]];
    aa.insert(a[i]);
  }
  a[0] = x;
  M[x];
  aa.insert(x);
  x = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> b[i];
    x ^= b[i];
    M[b[i]];
    bb.insert(b[i]);
  }
  b[0] = x;
  M[x];
  bb.insert(x);
  if (aa != bb) {
    cout << -1 << '\n';
    return 0;
  }
  int h = 0;
  for (auto &u : M) {
    u.second = ++h;
  }
  int e = 0;
  for (int i = 0; i <= n; ++i) {
    a[i] = M[a[i]];
    b[i] = M[b[i]];
    if (a[i] != b[i]) {
      adj[a[i]].push_back(b[i]);
      adj[b[i]].push_back(a[i]);
      ++e;
    }
  }
  if (e == 0) {
    cout << 0 << '\n';
    return 0;
  }
  int c = 0;
  for (int i = 1; i <= h; ++i) {
    if (vis[i] || adj[i].size() == 0) continue;
    ++c;
    dfs(i);
  }
  int ans = e - c;
  if (adj[a[0]].size() != 0) {
    ans += 2 * (c - 1);
    ++ans;
  } else {
    ans += 2 * c;
  }
  cout << ans << endl;
  return 0;
}
