%:pragma GCC optimize("Ofast")
  %:pragma GCC optimize("inline")
#include <bits/stdc++.h>
#define int long long
#define rint register int
using namespace std;

template<typename tp> inline void read(tp &x) {
  x = 0; char c = getchar(); int f = 0;
  for (; c < '0' || c > '9'; f |= c == '-', c = getchar());
  for (; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar());
  if (f) x = -x;
}
const int N = 3e5 + 233;
int n, id = 0;
int a[N], b[N], ida[N], idb[N], pre[N];
multiset<int> s;
map <int, vector <int> > veca, vecb;
map <int, int> cnt;
inline int find(int x) {
  return x == pre[x] ? x : pre[x] = find(pre[x]);
}

main(void) {
  read(n);
  int t = 0;
  for (int i = 1; i <= n; i ++)
    read(a[i]), t ^= a[i];
  a[n + 1] = t;
  for (int i = 1; i <= n; i ++)
    read(b[i]);
  for (int i = 1; i <= n + 1; i ++)
    s.insert(a[i]);
  for (int i = 1; i <= n; i ++) {
    if (!s.count(b[i]))
      return puts("-1"), 0;
    s.erase(s.lower_bound(b[i]));
  }
  b[++ n] = *s.begin();
  for (int i = 1; i <= n; i ++)
    if (a[i] != b[i]) {
      veca[a[i]].push_back(i);
      vecb[b[i]].push_back(i);
    }
  for (auto i : veca) {
    auto a = i.second;
    auto b = vecb[i.first];
    for (int k = 0; k < a.size(); k ++) {
      ++ id; pre[id] = id;
      ida[a[k]] = id;
      idb[b[k]] = id;
    }
  }
  
  for (int i = 1; i <= n; i ++)
    if (a[i] != b[i]) {
      // cout << ida[i] << " " << idb[i] << "\n";
      pre[find(ida[i])] = find(idb[i]);
    }
  for (int i = 1; i <= id; i ++)
    cnt[find(i)] ++;
  // for (int i = 1; i <= id; i ++)
  //   cout << pre[i] << " "; puts("");
  int ans = a[n] == b[n] ? -1 : -2;
  for (int i = 1; i <= id; i ++)
    if (find(i) == i)
      ans += cnt[i] + 1;
  cout << ans << "\n";
}
