#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, M = 2e6 + 5, mo = 1e9 + 7;
struct rec {
  int x, y;
} tmp;
bool operator<(rec a, rec b) { return a.x < b.x || a.x == b.x && a.y < b.y; }
multiset<rec> tr;
multiset<rec>::iterator it;
int n, i, x, a[N], b[N], c[N], d[N], pp, ans, y;
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", a + i);
    x ^= a[i];
  }
  for (i = 1; i <= n; i++) {
    scanf("%d", b + i);
    if (a[i] != b[i]) tr.insert({b[i], i});
  }
  while (tr.size()) {
    it = tr.lower_bound({x, 0});
    if (it != tr.end() && (*it).x == x) {
      y = (*it).y;
      x = a[y];
      a[y] = b[y];
      tr.erase(it);
      ans++;
    } else
      break;
  }
  for (i = 1; i <= n; i++) c[i] = a[i], d[i] = b[i];
  sort(c + 1, c + 1 + n);
  sort(d + 1, d + 1 + n);
  pp = 1;
  for (i = 1; i <= n; i++)
    if (c[i] != d[i]) pp = 0;
  if (!pp)
    printf("-1");
  else {
    while (tr.size()) {
      ans += 2;
      tmp = *tr.begin();
      tr.erase(tr.begin());
      a[tmp.y] = b[tmp.y];
      while (1) {
        it = tr.lower_bound({tmp.x, 0});
        if (it == tr.end() || (*it).x == tmp.x) break;
        tmp = *it;
        a[tmp.y] = b[tmp.y];
        tr.erase(it);
        ans++;
      }
    }
    printf("%d", ans);
  }
}
