#include <bits/stdc++.h>
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
using namespace std;
int n, cnt, num1[1000010], num2[1000010], ans;
int sum, d, head[1000010], fa[1000010];
bool vis[1000010];
struct node {
  int id, rank, val;
} a[1000010], b[1000010];
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline bool cmp(node x1, node x2) { return x1.val < x2.val; }
inline bool cmp1(node x1, node x2) { return x1.id < x2.id; }
inline int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
int main() {
  n = read();
  for (int i = 1; i <= n; i++) a[i].val = read(), a[i].id = i;
  for (int i = 1; i <= n; i++) b[i].val = read(), b[i].id = i;
  int s1 = 1, s2 = 1;
  a[0].val = -0x3f3f3f3f, b[0].val = -0x3f3f3f3f;
  sum = a[1].val;
  for (int i = 2; i <= n; i++) sum ^= a[i].val;
  bool flag = 0;
  int tt = b[1].val;
  for (int i = 2; i <= n; i++) tt ^= b[i].val;
  n++;
  a[n].id = n;
  a[n].val = sum;
  b[n].id = n;
  b[n].val = tt;
  sort(a + 1, a + 1 + n, cmp);
  sort(b + 1, b + 1 + n, cmp);
  while (s1 <= n && s2 <= n) {
    if (a[s1].val == a[s1 - 1].val) a[s1].rank = a[s1 - 1].rank, s1++;
    if (b[s2].val == b[s2 - 1].val) b[s2].rank = b[s2 - 1].rank, s2++;
    if (a[s1].val == b[s2].val) a[s1].rank = b[s2].rank = ++cnt, s1++, s2++;
    if (a[s1].val < b[s2].val) a[s1].rank = ++cnt, s1++;
    if (a[s1].val > b[s2].val) b[s2].rank = ++cnt, s2++;
  }
  while (s2 <= n) {
    if (b[s2].val == b[s2 - 1].val)
      b[s2].rank = b[s2 - 1].rank;
    else
      b[s2].rank = ++cnt;
    s2++;
  }
  while (s1 <= n) {
    if (a[s1].val == a[s1 - 1].val)
      a[s1].rank = a[s1 - 1].rank;
    else
      a[s1].rank = ++cnt;
    s1++;
  }
  sort(a + 1, a + 1 + n, cmp1);
  sort(b + 1, b + 1 + n, cmp1);
  for (int i = 1; i <= n; i++) num1[a[i].rank]++;
  for (int i = 1; i <= n - 1; i++) num2[b[i].rank]++;
  for (int i = 1; i <= cnt; i++) {
    if (num1[i] < num2[i]) {
      printf("-1");
      return 0;
    }
  }
  flag = 0;
  for (int i = 1; i <= cnt; i++) fa[i] = i;
  for (int i = 1; i <= n; i++) {
    if (a[i].rank != b[i].rank || i == n) {
      ans++;
      vis[a[i].rank] = vis[b[i].rank] = 1;
      int fx = find(a[i].rank);
      int fy = find(b[i].rank);
      fa[fx] = fy;
    }
  }
  for (int i = 1; i <= cnt; i++) {
    if (fa[i] == i && vis[i]) ans++;
  }
  printf("%d\n", ans - 2);
  return 0;
}
