#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
bool chkmin(int &x, int y) { return x > y ? x = y, 1 : 0; }
bool chkmax(int &x, int y) { return x < y ? x = y, 1 : 0; }
int rd() {
  int ret = 0;
  bool fl = 0;
  char ch = 0;
  while (!isdigit(ch)) fl |= (ch == '-'), ch = getchar();
  while (isdigit(ch)) ret = (ret << 1) + (ret << 3) + (ch ^ 48), ch = getchar();
  return fl ? -ret : ret;
}
vector<int> e[N];
bool vis[N], g[N];
void dfs(int u) {
  vis[u] = 1;
  for (int i = 0; i <= (signed)e[u].size() - 1; ++i) {
    int v = e[u][i];
    if (vis[v]) continue;
    dfs(v);
  }
}
int a[N], b[N], l[N], h[N];
int main() {
  int n = rd(), cnt = 0;
  for (int i = 1; i <= n; ++i) a[i] = rd(), l[i] = a[i], a[0] ^= a[i];
  for (int i = 1; i <= n; ++i) b[i] = rd(), h[i] = b[i], b[0] ^= b[i];
  l[0] = a[0], h[0] = b[0];
  sort(l, l + n + 1), sort(h, h + n + 1);
  int z = unique(l, l + n + 1) - l, p = unique(h, h + n + 1) - h;
  for (int i = 1; i <= n; ++i)
    if (l[i] != h[i]) return puts("-1"), 0;
  for (int i = 1; i <= n; ++i) {
    a[i] = lower_bound(l, l + z, a[i]) - l;
    b[i] = lower_bound(h, h + p, b[i]) - h;
  }
  g[a[0]] = g[b[0]] = 1;
  e[a[0]].push_back(b[0]);
  for (int i = 1; i <= n; ++i) {
    if (a[i] == b[i]) continue;
    cnt++;
    g[a[i]] = g[b[i]] = 1;
    e[a[i]].push_back(b[i]);
  }
  for (int i = 0; i <= n; ++i) {
    if (g[i] && (vis[i] == 0)) dfs(i), cnt++;
  }
  printf("%d\n", max(cnt - 1, 0));
}
