#include <bits/stdc++.h>
using namespace std;
const int RLEN = 1 << 18 | 1;
inline char nc() {
  static char ibuf[RLEN], *ib, *ob;
  (ib == ob) && (ob = (ib = ibuf) + fread(ibuf, 1, RLEN, stdin));
  return (ib == ob) ? -1 : *ib++;
}
inline int rd() {
  char ch = nc();
  int i = 0, f = 1;
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = nc();
  }
  while (isdigit(ch)) {
    i = (i << 1) + (i << 3) + ch - '0';
    ch = nc();
  }
  return i * f;
}
const int N = 1e5 + 50;
int n, a[N], b[N], step;
map<int, int> s;
set<pair<int, int> > pos[N];
set<pair<int, int> > need;
inline void shrink() {
  set<pair<int, int> >::iterator it =
      need.lower_bound(pair<int, int>(a[n + 1], 0));
  if (it == need.end() || it->first != a[n + 1]) return;
  int p = it->second;
  pos[a[p]].erase(pair<int, int>(b[p], p));
  need.erase(pair<int, int>(b[p], p));
  swap(a[p], a[n + 1]);
  ++step;
  shrink();
}
inline void lsh(int *c) {
  vector<int> vec;
  for (int i = 1; i <= n + 1; i++) vec.push_back(c[i]);
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  for (int i = 1; i <= n + 1; i++)
    c[i] = lower_bound(vec.begin(), vec.end(), c[i]) - vec.begin() + 1;
}
int main() {
  n = rd();
  for (int i = 1; i <= n; i++) a[i] = rd(), a[n + 1] ^= a[i], ++s[a[i]];
  ++s[a[n + 1]];
  for (int i = 1; i <= n; i++) b[i] = rd(), b[n + 1] ^= b[i], --s[b[i]];
  --s[b[n + 1]];
  for (auto v : s)
    if (v.second) return puts("-1"), 0;
  lsh(a);
  lsh(b);
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i])
      pos[a[i]].insert(pair<int, int>(b[i], i)),
          need.insert(pair<int, int>(b[i], i));
  shrink();
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) {
      set<pair<int, int> >::iterator it =
          pos[b[i]].lower_bound(pair<int, int>(a[i], 0));
      if (it == pos[b[i]].end()) --it;
      int p = it->second;
      pos[a[i]].erase(pair<int, int>(b[i], i));
      pos[a[p]].erase(pair<int, int>(b[p], p));
      need.erase(pair<int, int>(b[i], i));
      need.erase(pair<int, int>(b[p], p));
      swap(a[p], a[n + 1]);
      swap(a[i], a[n + 1]);
      step += 2;
      if (a[p] != b[p]) {
        pos[a[p]].insert(pair<int, int>(b[p], p));
        need.insert(pair<int, int>(b[p], p));
      }
      shrink();
    }
  cout << step << '\n';
}
