#include <bits/stdc++.h>
using namespace std;
int vi[500005], head[50005];
int top;
struct node {
  int u, v, nxt;
} e[50005];
int lsh[2100000], cnt, num[2100000];
int n, a[50005], b[50055], fa[50005], a1[52005], b1[50005];
int ti[500005];
int date[500005];
int yh;
int ans;
int get(int x) {
  if (fa[x] != x) fa[x] = get(fa[x]);
  return fa[x];
}
void add(int x, int y) {
  top++;
  e[top].u = x;
  e[top].v = y;
  e[top].nxt = head[x];
  head[x] = top;
}
int dfs(int x, int s) {
  if (s == 1) ans++;
  for (int i = head[x]; i; i = e[i].nxt)
    if (e[i].v != 0 && vi[e[i].v] == 0) {
      vi[e[i].v] = 1;
      ans++;
      dfs(e[i].v, s + 1);
    }
}
void unionn(int x, int y) { fa[x] = y; }
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a1[i] = a[i];
    yh ^= a[i];
  }
  a[n + 1] = yh;
  a1[n + 1] = yh;
  yh = 0;
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    b1[i] = b[i];
    yh ^= b[i];
  }
  b[n + 1] = yh;
  b1[n + 1] = yh;
  sort(a1 + 1, a1 + n + 2), sort(b1 + 1, b1 + n + 2);
  for (int i = 1; i <= n + 1; i++)
    if (a1[i] != b1[i]) {
      cout << "-1";
      return 0;
    }
  n++;
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= n; i++) lsh[i] = a[i];
  sort(lsh + 1, lsh + 1 + n);
  cnt = unique(lsh + 1, lsh + n + 1) - lsh - 1;
  for (int i = 1; i <= n; i++)
    a[i] = lower_bound(lsh + 1, lsh + cnt + 1, a[i]) - lsh;
  memset(lsh, 0, sizeof(lsh));
  for (int i = 1; i <= n; i++) lsh[i] = b[i];
  sort(lsh + 1, lsh + n + 1);
  cnt = unique(lsh + 1, lsh + n + 1) - lsh - 1;
  for (int i = 1; i <= n; i++)
    b[i] = lower_bound(lsh + 1, lsh + cnt + 1, b[i]) - lsh;
  int num = 0;
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i] || (i == n)) {
      date[++num] = a[i];
      date[++num] = b[i];
      if (i < n) ans++;
    }
  if (ans == 0) return puts("0"), 0;
  sort(date + 1, date + num + 1);
  num = unique(date + 1, date + num + 1) - date - 1;
  for (int i = 1; i <= num; i++) fa[i] = i;
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) {
      a[i] = lower_bound(date + 1, date + num + 1, a[i]) - date,
      b[i] = lower_bound(date + 1, date + num + 1, b[i]) - date;
      if (!ti[a[i]]) ti[a[i]] = 1;
      if (!ti[b[i]]) ti[b[i]] = 1;
      unionn(get(a[i]), get(b[i]));
    }
  for (int i = 1; i <= num; i++)
    if (fa[i] == i) ans++;
  cout << ans - 1;
  return 0;
}
