#include <bits/stdc++.h>
using namespace std;
const int N = 200005, mod = 1000000007;
int n, m, a[N], b[N], d[N], pos[N], sz, c, s;
bool vis[N];
map<int, int> mp;
struct edge {
  int y, nex;
} g[N * 2];
void init(int x, int y) {
  x = mp[x], y = mp[y];
  d[x]++, d[y]--;
  g[++sz] = (edge){y, pos[x]}, pos[x] = sz;
}
void dfs(int x) {
  vis[x] = 1;
  s++;
  for (int i = pos[x]; i; i = g[i].nex)
    if (!vis[g[i].y]) dfs(g[i].y);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (!mp[a[i]]) mp[a[i]] = ++m;
    a[0] ^= a[i];
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    if (!mp[b[i]]) mp[b[i]] = ++m;
    b[0] ^= b[i];
    if (a[i] != b[i]) init(b[i], a[i]);
  }
  if (!mp[a[0]]) mp[a[0]] = ++m;
  if (!mp[b[0]]) mp[b[0]] = ++m;
  if (a[0] != b[0]) init(b[0], a[0]), sz--;
  for (int i = 1; i <= m; i++)
    if (d[i]) {
      puts("-1");
      return 0;
    }
  for (int i = 1; i <= m; i++)
    if (!vis[i]) {
      s = 0;
      dfs(i);
      if (s > 1) c++;
    }
  if (c > 1) c--;
  printf("%d\n", sz + c);
  return 0;
}
