#include <bits/stdc++.h>
using namespace std;
const double pi =
    3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;
const int MR = 1e5 + 10;
int a[MR], b[MR];
int main() {
  map<int, set<pair<int, int>>> Ma, Mb;
  int N;
  scanf("%d", &N);
  int x = 0;
  for (int i = 0; i < (N); ++i) {
    scanf("%d", &a[i]);
    x ^= a[i];
  }
  set<int> pos;
  for (int i = 0; i < (N); ++i) {
    scanf("%d", &b[i]);
    if (a[i] != b[i]) {
      int pr = Mb[a[i]].empty() ? 1 : (--Mb[a[i]].end())->first + 1;
      Ma[a[i]].insert(make_pair(pr, i));
      Mb[b[i]].insert(make_pair(pr, i));
      pos.insert(i);
    }
  }
  int res = 0;
  while (!pos.empty()) {
    while (!Mb[x].empty()) {
      auto tmp = *(--Mb[x].end());
      Mb[x].erase(tmp);
      for (auto p : Ma[a[tmp.second]])
        if (p.second == tmp.second) {
          Ma[a[tmp.second]].erase(p);
          break;
        }
      swap(a[tmp.second], x);
      res++;
      pos.erase(tmp.second);
    }
    if (pos.empty()) break;
    int tmp = *pos.begin();
    if (Ma[b[tmp]].empty()) {
      printf("-1\n");
      return 0;
    }
    auto tmpa = *(--Ma[b[tmp]].end());
    for (auto p : Ma[b[tmp]])
      if (p.second == tmpa.second) {
        Ma[b[tmp]].erase(p);
        break;
      }
    Ma[x].insert(tmpa);
    swap(x, a[tmpa.second]);
    res++;
  }
  printf("%d\n", res);
  return 0;
}
