#include <bits/stdc++.h>
using namespace std;
const int maxn = 300005;
const int maxm = maxn * 2;
const int inf = 0x3f3f3f3f;
int n, a[maxn], b[maxn];
int vis[maxn];
int fa[maxn];
int fi(int x) {
  if (fa[x] == x)
    return x;
  else
    return fa[x] = fi(fa[x]);
}
void uni(int x, int y) {
  int xi = fi(x), yi = fi(y);
  if (xi != yi) {
    fa[xi] = yi;
  }
  return;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[1 + n] ^= a[i];
    vis[a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    b[1 + n] ^= b[i];
    vis[b[i]]--;
  }
  vis[a[n + 1]]++;
  vis[b[n + 1]]--;
  for (int i = 0; i <= n; i++) {
    if (vis[i] != 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  int m = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] != b[i]) m++;
    uni(a[i], b[i]);
  }
  int ans = m - 1;
  for (int i = 1; i <= n; i++) {
    if (fi(i) == i && a[i] != b[i]) ans++;
  }
  cout << ans - 1 << endl;
  return 0;
}
