#include <bits/stdc++.h>
using namespace std;
int n, a[100011], b[100011], c[100011], d[100011], xr, ans;
bool check() {
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) return 0;
  return 1;
}
int main() {
  scanf("%d", &n);
  bool pd = 1;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), xr ^= a[i], c[i] = a[i];
  for (int i = 1; i <= n; i++)
    scanf("%d", &b[i]), pd = pd && (a[i] == b[i]), d[i] = b[i];
  if (pd) {
    printf("0\n");
    return 0;
  }
  c[n + 1] = xr;
  sort(c + 1, c + n + 2);
  sort(d + 1, d + n + 1);
  int cnt = 0;
  int l = 1;
  int r = 1;
  while (l <= n + 1 && r <= n) {
    if (c[l] == d[r]) {
      l++;
      r++;
    } else {
      cnt++;
      l++;
    }
    if (cnt >= 2) {
      printf("-1\n");
      return 0;
    }
  }
  bool pd1 = 0;
  while (true) {
    pd1 = 0;
    for (int i = 1; i <= n; i++)
      if (b[i] != a[i] && b[i] == xr) {
        int tmp = xr;
        xr = a[i];
        a[i] = tmp;
        ans++;
        i = n + 1;
        pd1 = 1;
      }
    if (!pd1) {
      while (!check()) {
        for (int i = 1; i <= n; i++) {
          if (a[i] != b[i]) {
            int tmp = xr;
            xr = a[i];
            a[i] = tmp;
            ans++;
            i = n + 1;
          }
        }
        bool pd2 = 0;
        while (true) {
          pd2 = 0;
          for (int i = 1; i <= n; i++)
            if (b[i] != a[i] && b[i] == xr) {
              int tmp = xr;
              xr = a[i];
              a[i] = tmp;
              ans++;
              i = n + 1;
              pd2 = 1;
            }
          if (!pd2) break;
          if (ans >= 8000000) {
            printf("-1\n");
            return 0;
          }
        }
      }
      break;
    }
  }
  printf("%d\n", ans);
}
