#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
const int oo = 0x3f3f3f3f;
const int maxn = 100100;
int n;
int a[maxn + 5], b[maxn + 5];
int tmpa[maxn + 5], tmpb[maxn + 5];
int fa[maxn + 5];
int get(int first) {
  return fa[first] == first ? first : fa[first] = get(fa[first]);
}
int main() {
  scanf("%d", &n);
  int suma = 0;
  for (int i = (0), i_end_ = (n); i < i_end_; ++i)
    scanf("%d", a + i), suma ^= a[i];
  a[n] = suma;
  int sumb = 0;
  for (int i = (0), i_end_ = (n); i < i_end_; ++i)
    scanf("%d", b + i), sumb ^= b[i];
  b[n] = sumb;
  memcpy(tmpa, a, sizeof tmpa);
  memcpy(tmpb, b, sizeof tmpb);
  sort(tmpa, tmpa + n + 1);
  sort(tmpb, tmpb + n + 1);
  for (int i = (0), i_end_ = (n + 1); i < i_end_; ++i)
    if (tmpa[i] != tmpb[i]) {
      printf("-1\n");
      return 0;
    }
  for (int i = (0), i_end_ = (n); i < i_end_; ++i)
    a[i] = lower_bound(tmpa, tmpa + n + 1, a[i]) - tmpa;
  for (int i = (0), i_end_ = (n); i < i_end_; ++i)
    b[i] = lower_bound(tmpa, tmpa + n + 1, b[i]) - tmpa;
  for (int i = (0), i_end_ = (n + 1); i < i_end_; ++i) fa[i] = i;
  set<int> all;
  int ans = 0;
  for (int i = (0), i_end_ = (n); i < i_end_; ++i)
    if (a[i] != b[i]) {
      ++ans;
      all.insert(a[i]), all.insert(b[i]);
      fa[get(a[i])] = get(b[i]);
    }
  set<int> comp;
  for (auto u : all) comp.insert(get(u));
  ans += (int((comp).size())) - 1;
  chkmax(ans, 0);
  printf("%d\n", ans);
  return 0;
}
