#include <bits/stdc++.h>
using namespace std;
int pos[100001];
int ed[100001];
int ed1[100001];
int r[100001];
long long int a[100001];
long long int b[100001];
int main() {
  int i, j;
  int n;
  cin >> n;
  pair<long long int, int> c[n + 1];
  pair<long long int, int> d[n + 1];
  long long int xo = 0;
  int dis = 0;
  int in = 0;
  int o = 0;
  for (i = 1; i <= n; i++) cin >> a[i];
  for (i = 1; i <= n; i++) cin >> b[i];
  for (i = 1; i <= n; i++) {
    xo = xo ^ a[i];
    if (a[i] == b[i]) {
      pos[i] = 1;
      o++;
    }
    c[i].first = a[i];
    c[i].second = i;
    d[i].first = b[i];
    d[i].second = i;
  }
  sort(c + 1, c + n + 1);
  sort(d + 1, d + n + 1);
  int in2 = 1;
  int yy = 0;
  for (i = 1; i <= n; i++) {
    if (in2 > n) break;
    if (c[i].first == d[in2].first) {
      int g, h;
      g = c[i].second;
      h = d[in2].second;
      if (pos[g] || pos[h]) {
        if (pos[g])
          if (pos[h]) {
            in2++;
            if (pos[g] == 0) i--;
          }
        continue;
      }
      yy++;
      in2++;
      ed[g] = h;
      ed1[h] = g;
    } else {
      if (d[in2].first > c[i].first)
        continue;
      else {
        in2++;
        i--;
      }
    }
  }
  dis = o + yy;
  dis = n - dis;
  int in1 = 0;
  for (i = 1; i <= n; i++) {
    if (pos[i]) continue;
    if (ed1[i] == 0) in = i;
    if (ed[i] == 0) in1 = i;
  }
  if (dis >= 2 || ((dis == 1) && (xo != b[in]))) {
    cout << -1 << endl;
    return 0;
  }
  int ans = 0;
  ed[in1] = in;
  for (i = 1; i <= n; i++) {
    if (ed[i] == 0) continue;
    if (r[i] == 0) {
      int fl = 1;
      int ss = 0;
      while (r[i] == 0) {
        r[i] = 1;
        if (i == in1) fl = 0;
        if (a[i] == xo) fl = 0;
        i = ed[i];
        ss++;
      }
      ans = ans + fl + ss;
    }
  }
  cout << ans << endl;
  return 0;
}
