#include <bits/stdc++.h>
using namespace std;
int n, s[1000010], t[1000010], fa[1000010], x = 0, y = 0, m = 0, times[1000010],
                                            arr[1000010], ans = 0, sx[1000010],
                                            sy[1000010];
bool visit[1000010], flag = false;
int find(int x) {
  if (fa[x] == x)
    return x;
  else
    return fa[x] = find(fa[x]);
}
void unionn(int x, int y) { fa[find(x)] = find(y); }
int main() {
  memset(times, 0, sizeof(times)), memset(sx, 0, sizeof(sx)),
      memset(sy, 0, sizeof(sy));
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &s[i]);
    x ^= s[i];
    times[++m] = arr[m] = sx[m] = s[i];
  }
  times[++m] = arr[m] = sx[m] = x;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &t[i]);
    y ^= t[i];
    times[++m] = arr[m] = sy[m] = t[i];
    if (t[i] != s[i]) flag = true;
  }
  if (!flag) {
    cout << "0" << endl;
    return 0;
  }
  times[++m] = arr[m] = y;
  sort(times + 1, times + 1 + n), sort(sx + 1, sx + n + 2),
      sort(sy + 1, sy + n + 1);
  flag = false;
  for (int i = 1; i <= n; ++i)
    if (sx[i] != sy[i]) flag = true;
  if (flag) {
    cout << "-1" << endl;
    return 0;
  }
  int o = unique(times + 1, times + 1 + n) - times - 1;
  for (int i = 1; i <= m; ++i)
    arr[i] = lower_bound(times + 1, times + 1 + o, arr[i]) - times;
  for (int i = 1; i <= m; ++i) fa[i] = i;
  for (int i = 1; i <= n + 1; ++i)
    if (arr[i] != arr[i + n + 1] && find(arr[i]) != find(arr[i + n + 1]))
      unionn(arr[i], arr[i + n + 1]), ++ans;
  memset(visit, false, sizeof(visit));
  for (int i = 1; i <= n + 1; ++i)
    if (!visit[arr[i]] && arr[i] == find(arr[i]) && arr[i] != arr[i + n + 1])
      ++ans, visit[arr[i]] = true;
  printf("%d\n", ans - 1);
  return 0;
}
