#include <bits/stdc++.h>
using namespace std;
int mpow(int base, int exp);
void ipgraph(int m);
int dfs(int u);
const int mod = 1000000007;
const int N = 3e5, M = N;
vector<int> g[N];
int a[N];
int b[N];
int vis[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int i, n, k, j;
  cin >> n;
  int x = 0;
  multiset<int> A;
  for (i = 0; i < n; i++) cin >> a[i + 1], x ^= a[i + 1], A.insert(a[i + 1]);
  a[0] = x;
  A.insert(x);
  for (i = 0; i < n; i++) {
    cin >> b[i + 1];
    if (A.find(b[i + 1]) == A.end()) {
      cout << -1 << endl;
      return 0;
    }
    A.erase(A.find(b[i + 1]));
  }
  b[0] = *A.begin();
  for (i = 0; i < n + 1; i++) {
    g[b[i]].push_back(a[i]);
  }
  int ans = max(1, dfs(a[0])) - 1;
  if (a[0] == b[0] and ans) ans++;
  for (i = 1; 1 < n + 1 ? i < n + 1 : i > n + 1; 1 < n + 1 ? i += 1 : i -= 1) {
    if (!vis[a[i]]) {
      x = dfs(a[i]);
      if (x) ans += x + 1;
    }
  }
  cout << ans << endl;
  return 0;
}
int dfs(int u) {
  vis[u] = 1;
  int ans = 0;
  for (int v : g[u]) {
    if (v == u) continue;
    ans++;
    if (vis[v]) continue;
    ans += dfs(v);
  }
  return ans;
}
