#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
using namespace std;
const long long N = 2e5 + 100;
vector<long long> g[N];
long long long long a[N], b[N];
map<long long, long long> mp;
long long long long vis[N];
long long long long ts = 0;
void dfs(long long long long v) {
  vis[v] = 1;
  for (auto u : g[v]) {
    if (!vis[u]) dfs(u);
  }
  return;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long long long n;
  cin >> n;
  long long long long x = 0;
  vector<long long> c, d;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    x ^= a[i];
    c.push_back(a[i]);
  }
  a[n + 1] = x;
  c.push_back(x);
  x = 0;
  for (long long i = 1; i <= n; i++) {
    cin >> b[i];
    x ^= b[i];
    d.push_back(b[i]);
  }
  b[n + 1] = x;
  n++;
  d.push_back(x);
  sort(c.begin(), c.end());
  sort(d.begin(), d.end());
  for (long long i = 0; i < n; i++)
    if (c[i] != d[i]) return cout << -1 << '\n', 0;
  ;
  for (long long i = 0; i < n; i++) mp[c[i]] = i + 1;
  for (long long i = 1; i <= n; i++) a[i] = mp[a[i]], b[i] = mp[b[i]];
  for (long long i = 1; i <= n; i++) {
    if (a[i] == b[i]) continue;
    g[b[i]].push_back(a[i]);
    ts++;
  }
  long long long long cnt = 0;
  for (long long i = 1; i <= n; i++) {
    if (g[i].size() && !vis[i]) dfs(i), cnt++;
  }
  if (a[n] != b[n]) cnt--;
  long long long long ans = cnt + ts - 1;
  if (ans == -1) ans = 0;
  cout << ans << '\n';
}
