#include <bits/stdc++.h>
const int MAXN = 1e5 + 10;
int N, val[MAXN], vis[MAXN];
std::map<int, std::set<int> > org_pos;
namespace FastIO {
template <typename T>
void read(T& x) {
  x = 0;
  register char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    ;
  for (; ch >= '0' && ch <= '9';
       x = (x << 3) + (x << 1) + (ch ^ '0'), ch = getchar())
    ;
}
}  // namespace FastIO
int main() {
  using FastIO::read;
  read(N);
  int _xor = 0;
  for (int i = 1, vali; i <= N; i++) {
    read(vali);
    org_pos[vali].insert(i);
    _xor ^= vali;
  }
  org_pos[_xor].insert(0);
  for (int i = 1; i <= N; i++) {
    read(val[i]);
    std::map<int, std::set<int> >::iterator it = org_pos.find(val[i]);
    if (it == org_pos.end() || it->second.empty()) {
      puts("-1");
      return 0;
    }
    std::set<int>::iterator itt = it->second.find(i);
    if (itt != it->second.end()) vis[i] = 1, it->second.erase(itt);
  }
  int ans = 0;
  vis[0] = 1;
  for (int i = 1, tag = 2; i <= N; ++tag, i++) {
    if (vis[i]) continue;
    int step = 0, j;
    for (j = i; !vis[j]; ++step) {
      if (org_pos[val[j]].empty()) {
        puts("-1");
        return 0;
      }
      vis[j] = tag;
      int tmp = *org_pos[val[j]].begin();
      org_pos[val[j]].erase(org_pos[val[j]].begin());
      j = tmp;
    }
    if (vis[j] == tag) ++step;
    ans += step;
  }
  printf("%d\n", ans);
  return 0;
}
