#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
vector<int> v;
set<int> pos[N];
int n, sum, a[N], b[N], cnt[N];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    sum ^= a[i];
    v.push_back(a[i]);
  }
  a[0] = sum;
  v.push_back(sum);
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  for (int i = 0; i <= n; ++i) {
    a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
    ++cnt[a[i]];
  }
  int ya = 1;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", b + i);
    b[i] = lower_bound(v.begin(), v.end(), b[i]) - v.begin();
    if (cnt[b[i]] == 0) {
      ya = 0;
    } else {
      --cnt[b[i]];
    }
  }
  int tot = 0;
  for (int i = 0; i < N; ++i) {
    tot += cnt[i];
  }
  if (tot > 1) {
    ya = 0;
  }
  if (ya == 0) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i <= n; ++i) {
    if (a[i] == b[i]) continue;
    pos[a[i]].insert(i);
  }
  int moves = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] == b[i]) continue;
    if (a[0] == b[i]) {
      pos[a[i]].erase(i);
      swap(a[0], a[i]);
      ++moves;
      continue;
    }
    assert(!pos[b[i]].empty());
    int at = *pos[b[i]].begin();
    pos[b[i]].erase(at);
    pos[a[0]].insert(at);
    pos[a[i]].erase(i);
    swap(a[0], a[at]);
    swap(a[0], a[i]);
    moves += 2;
  }
  for (int i = 1; i <= n; ++i) {
    if (a[i] != b[i]) {
      puts("-1");
      return 0;
    }
  }
  printf("%d\n", moves);
  return 0;
}
