#include <bits/stdc++.h>
using namespace std;
const int N = 55;
const int M = 1e5 + 1;
struct nd {
  int a, b;
} v[N];
bool cmp(nd c, nd d) { return c.b < d.b; }
int n, m;
int find(int x) {
  int l = 1, r = n;
  while (l != r) {
    int mid = l + r + 1 >> 1;
    if (v[mid].b <= x)
      l = mid;
    else
      r = mid - 1;
  }
  return l;
}
int main() {
  int tmp = 0;
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> v[i].a;
  for (int i = 1; i <= n; i++) cin >> v[i].b;
  for (int i = 1; i <= n; ++i)
    if (v[i].a == v[i].b) tmp++;
  sort(v + 1, v + n + 1, cmp);
  int x = 0;
  for (int i = 1; i <= n; ++i) x ^= v[i].a;
  int tot = 0;
  while (1) {
    bool flag = 1;
    for (int i = 1; i <= min(50, n); ++i) {
      if (v[i].a != v[i].b) {
        flag = 0;
        break;
      }
    }
    for (int i = n; i >= max(1, n - 50); --i) {
      if (v[i].a != v[i].b) {
        flag = 0;
        break;
      }
    }
    if (flag) break;
    int t = find(x);
    tot++;
    if (v[t].b == x)
      x ^= v[t].a, v[t].a = v[t].b, x ^= v[t].a;
    else {
      cout << "-1";
      return 0;
    }
  }
  cout << tot;
  return 0;
}
