#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 100;
int n, a[MAXN], b[MAXN], sec[MAXN], sz, xx;
int in[MAXN], out[MAXN];
vector<int> adj[MAXN];
int cc, cnt;
int inV, outV;
bool vis[MAXN];
int get(int &x) { x = lower_bound(sec, sec + sz, x) - sec; }
void dfs(int v) {
  vis[v] = 1;
  cnt += in[v];
  if (in[v] ^ out[v]) {
    cc += abs(in[v] - out[v]);
    if (in[v] > out[v])
      inV = v;
    else
      outV = v;
  }
  for (int u : adj[v])
    if (!vis[u]) dfs(u);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i], xx ^= a[i], sec[sz++] = a[i];
  sec[sz++] = xx;
  for (int i = 0; i < n; i++) cin >> b[i], sec[sz++] = b[i];
  sort(sec, sec + sz);
  sz = unique(sec, sec + sz) - sec;
  for (int i = 0; i < n; i++) get(a[i]);
  for (int i = 0; i < n; i++) get(b[i]);
  get(xx);
  for (int i = 0; i < n; i++)
    if (a[i] ^ b[i]) {
      out[b[i]]++;
      in[a[i]]++;
      adj[a[i]].push_back(b[i]);
      adj[b[i]].push_back(a[i]);
    }
  int ans = 0;
  bool done = 0;
  for (int i = 0; i < sz; i++)
    if (!vis[i] && adj[i].size()) {
      cc = cnt = 0;
      dfs(i);
      if (cc > 2) {
        cout << "-1\n";
        return 0;
      }
      if (cc == 0)
        ans += cnt + 1;
      else {
        ans += cnt;
        if (done) {
          cout << "-1\n";
          return 0;
        }
        done = 1;
        if (xx != outV) {
          cout << "-1\n";
          return 0;
        }
      }
    }
  if (!done)
    for (int i = 0; i < n; i++)
      if (b[i] == xx) {
        ans--;
        break;
      }
  cout << ans << "\n";
  return 0;
}
