#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:250000000")
using namespace std;
int n;
int a[100000];
int b[100000];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
  for (int i = 0; i < n; ++i) scanf("%d", &b[i]);
  int x = 0;
  for (int i = 0; i < n; ++i) x ^= a[i];
  map<int, vector<int>> p;
  for (int i = 0; i < n; ++i)
    if (a[i] != b[i]) p[b[i]].push_back(i);
  int res = 0;
  while (p.count(x)) {
    int pos = p[x].back();
    p[x].pop_back();
    if (p[x].empty()) p.erase(x);
    x = a[pos];
    res++;
  }
  map<int, int> id;
  vector<int> pos;
  for (auto &y : p) {
    for (int i : y.second) {
      id[i] = id.size();
      pos.push_back(i);
    }
  }
  vector<int> perm;
  for (int i : pos) {
    if (!p.count(a[i])) {
      printf("-1\n");
      return 0;
    }
    perm.push_back(id[p[a[i]].back()]);
    p[a[i]].pop_back();
    if (p[a[i]].empty()) p.erase(a[i]);
  }
  vector<int> used(perm.size(), 0);
  res += 3 * perm.size();
  for (int i = 0; i < perm.size(); ++i) {
    if (used[i]) continue;
    int j = i;
    while (!used[j]) {
      used[j] = 1;
      j = perm[j];
    }
    res -= 3;
  }
  printf("%d\n", res);
  return 0;
}
