#include <bits/stdc++.h>
using namespace std;
const int N = 100002;
int n;
struct data {
  int x, y;
} a[N];
map<int, int> mp;
int f[N], sz[N];
vector<int> w[N];
int b[N];
void read(int &x) {
  char ch = getchar();
  x = 0;
  for (; ch < '0' || ch > '9'; ch = getchar())
    ;
  for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
}
int gf(int x) { return f[x] == x ? x : f[x] = gf(f[x]); }
void unio(int x, int y) {
  int fx = gf(x), fy = gf(y);
  if (fx != fy) {
    f[fx] = fy;
    sz[fy] += sz[fx];
  }
}
int main() {
  read(n);
  int xr = 0;
  for (int i = 1; i <= n; i++) read(a[i].x), xr ^= a[i].x, mp[a[i].x]++;
  a[n + 1].x = xr;
  mp[xr]++;
  int tot = 0;
  int flag = 0;
  for (int i = 1; i <= n; i++) {
    read(a[i].y);
    if (!mp.count(a[i].y)) {
      printf("-1\n");
      return 0;
    }
    mp[a[i].y]--;
    if (mp[a[i].y] == 0) mp.erase(a[i].y);
    if (a[i].y == xr && a[i].x != a[i].y) flag = 1;
  }
  a[n + 1].y = mp.begin()->first;
  mp.clear();
  for (int i = 1; i <= n + 1; i++)
    if (a[i].x != a[i].y) mp[a[i].x] = 1, mp[a[i].y] = 1;
  tot = 0;
  for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++)
    it->second = ++tot;
  for (int i = 1; i <= n + 1; i++)
    if (a[i].x != a[i].y) a[i].x = mp[a[i].x], a[i].y = mp[a[i].y];
  for (int i = 1; i <= tot; i++) f[i] = i;
  for (int i = 1; i <= n + 1; i++)
    if (a[i].x != a[i].y) sz[a[i].x]++;
  for (int i = 1; i <= n + 1; i++)
    if (a[i].x != a[i].y) {
      unio(a[i].x, a[i].y);
    }
  int ans = 0;
  for (int i = 1; i <= tot; i++)
    if (gf(i) == i) ans += sz[i] + 1;
  if (flag) ans -= 2;
  cout << ans << endl;
  return 0;
}
