#include <bits/stdc++.h>
using namespace std;
int buf[100005], bcnt, *x[100005];
bool cmp(int *a, int *b) { return (*a) < (*b); }
void lsh(int *a, int sz) {
  for (int i = 1; i <= sz; i++) x[i] = &a[i];
  bcnt = 0;
  sort(x + 1, x + sz + 1, cmp);
  buf[1] = ++bcnt;
  for (int i = 2; i <= sz; i++)
    buf[i] = *x[i] == *x[i - 1] ? buf[i - 1] : ++bcnt;
  for (int i = 1; i <= sz; i++) *x[i] = buf[i];
}
int n;
int bcj[100005];
int ff(int x) { return bcj[x] == x ? x : bcj[x] = ff(bcj[x]); }
void connect(int x, int y) {
  if (ff(x) == ff(y)) return;
  bcj[ff(x)] = ff(y);
}
int a[100005], b[100005];
int c[100005], d[100005], m;
int main() {
  scanf("%d", &n);
  int ta = 0, tb = 0;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), ta ^= a[i];
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]), tb ^= b[i];
  n++;
  a[n] = ta;
  b[n] = tb;
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) c[++m] = a[i], d[m] = b[i];
  int fl1 = 0, fl2 = 0;
  if (a[n] == b[n]) fl1 = 1;
  for (int i = 1; i <= m; i++)
    if (c[i] == a[n]) fl2 = 1;
  sort(a + 1, a + n + 1);
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) return puts("-1"), 0;
  int ans = 0;
  if (fl1 == 1 && fl2 == 1) ans = -1;
  if (fl1 == 0) ans -= 2;
  ans += m;
  lsh(c, m);
  lsh(d, m);
  for (int i = 1; i <= m; i++) bcj[i] = i;
  for (int i = 1; i <= m; i++) connect(c[i], d[i]);
  for (int i = 1; i <= m; i++)
    if (ff(i) == i) ans++;
  cout << ans << endl;
  return 0;
}
