#include <bits/stdc++.h>
using namespace std;
struct P {
  int x, pos;
};
bool operator<(P a, P b) { return a.x < b.x; }
int n, a[100005], b[100005], pos[100005], nxt[100005];
pair<int, int> c[100005], d[100005];
set<P> s;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), a[n + 1] ^= a[i];
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]), b[n + 1] ^= b[i];
  for (int i = 1; i <= n + 1; i++)
    c[i] = make_pair(a[i], i), d[i] = make_pair(b[i], i);
  sort(c + 1, c + n + 2);
  sort(d + 1, d + n + 2);
  for (int i = 1, cnt = 0; i <= n + 1; i++) {
    if (i == 1 || c[i].first != c[i - 1].first) cnt++;
    a[c[i].second] = cnt;
    b[d[i].second] = cnt;
    if (c[i].first != d[i].first) {
      puts("-1");
      return 0;
    }
  }
  for (int i = n; i; i--) {
    nxt[i] = pos[b[i]];
    pos[b[i]] = i;
  }
  for (int i = n; i; i--)
    if (b[i] != a[i]) s.insert(P{b[i], i});
  int S = a[n + 1], ans = 0;
  set<P>::iterator t;
  while (s.size()) {
    t = s.find(P{S, 0});
    if (t != s.end()) {
      ans++;
      int pos = (*t).pos, x = (*t).x;
      s.erase(t);
      swap(a[pos], S);
      while (nxt[pos]) {
        pos = nxt[pos];
        if (a[pos] == b[pos]) continue;
        s.insert(P{x, pos});
        break;
      }
    } else {
      ans++;
      swap(S, a[(*s.begin()).pos]);
    }
  }
  printf("%d", ans);
  return 0;
}
