#include <bits/stdc++.h>
using namespace std;
const int max_n = 101111, inf = 1011111111;
int n, x, a[max_n], b[max_n];
vector<int> v, bad[max_n];
map<int, int> m;
void prec() {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  for (int i = 0; i < v.size(); ++i) {
    m[v[i]] = i;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    x ^= a[i];
    v.push_back(a[i]);
  }
  v.push_back(x);
  prec();
  for (int i = 0; i < n; ++i) {
    scanf("%d", &b[i]);
    if (!m.count(b[i])) {
      printf("%d\n", -1);
      return 0;
    }
  }
  x = m[x];
  set<int> all;
  for (int i = 0; i < n; ++i) {
    a[i] = m[a[i]];
    b[i] = m[b[i]];
    if (a[i] != b[i]) {
      bad[b[i]].push_back(i);
      all.insert(i);
    }
  }
  for (int i = 0; i < 3 * max_n; ++i) {
    if (all.size() == 0) {
      printf("%d\n", i);
      return 0;
    }
    if (bad[x].size()) {
      int pos = bad[x].back();
      bad[x].pop_back();
      all.erase(pos);
      int k = a[pos];
      a[pos] = x;
      x = k;
    } else {
      int pos = *all.rbegin();
      int k = a[pos];
      a[pos] = x;
      x = k;
    }
    if (0) {
      for (int j = 0; j < n; ++j) {
        cout << a[j] << " ";
      }
      cout << endl;
    }
  }
  printf("-1\n");
  return 0;
}
