#include <bits/stdc++.h>
using namespace std;
const int N = 100003;
int n, len, fa[N], a[N], b[N], ta[N], tb[N], ans;
inline int getfa(int x) { return x == fa[x] ? x : fa[x] = getfa(fa[x]); }
int main() {
  scanf("%d", &n);
  for (register int i = 1; i <= n; i++)
    scanf("%d", a + i), a[n + 1] ^= (ta[i] = a[i]);
  for (register int i = 1; i <= n; i++)
    scanf("%d", b + i), b[n + 1] ^= (tb[i] = b[i]);
  ++n;
  ta[n] = a[n];
  tb[n] = b[n];
  sort(ta + 1, ta + n + 1);
  sort(tb + 1, tb + n + 1);
  bool flag = true;
  for (register int i = 1; flag && i <= n; i++) flag &= (a[i] == b[i]);
  if (flag) {
    puts("0");
    return 0;
  }
  for (register int i = 1; i <= n; i++)
    if (ta[i] != tb[i]) {
      puts("-1");
      return 0;
    }
  len = unique(ta + 1, ta + n + 1) - ta - 1;
  for (register int i = 1; i <= len; i++) fa[i] = i;
  for (register int i = 1; i <= n; i++)
    if (i == n || a[i] != b[i]) {
      if (i < n) ++ans;
      a[i] = lower_bound(ta + 1, ta + len + 1, a[i]) - ta;
      b[i] = lower_bound(ta + 1, ta + len + 1, b[i]) - ta;
      int fa1 = getfa(a[i]), fa2 = getfa(b[i]);
      if (fa1 != fa2) fa[fa1] = fa2;
    }
  for (register int i = 1; i <= len; i++)
    if (a[i] != b[i] && fa[i] == i) ++ans;
  printf("%d", ans - 1);
}
