#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
  x = 0;
  static int p;
  p = 1;
  static char c;
  c = getchar();
  while (!isdigit(c)) {
    if (c == '-') p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - '0');
    c = getchar();
  }
  x *= p;
}
int n;
int A[100010], B[100010];
set<pair<int, int> > s;
map<int, int> mp;
int main() {
  read(n);
  int sum = 0, pd = 0;
  for (int i = 1; i <= n; i++) read(A[i]), sum ^= A[i], mp[A[i]]++;
  for (int i = 1; i <= n; i++) {
    read(B[i]);
    if (!mp.count(B[i])) {
      if (B[i] != sum)
        return puts("-1"), 0;
      else if (!pd)
        pd = 1;
      else
        return puts("-1"), 0;
    } else {
      mp[B[i]]--;
      if (!mp[B[i]]) mp.erase(B[i]);
    }
    if (A[i] != B[i]) s.insert(pair<int, int>(B[i], i));
  }
  {
    int ans = 0, stsum = 0, fir = 0;
    set<pair<int, int> >::iterator it, itt;
    it = s.lower_bound(pair<int, int>(sum, 0));
    if (it != s.end() && (*it).first == sum) {
      stsum = A[(*it).second];
      A[fir = (*it).second] = sum;
      ans++;
      s.erase(it);
    } else
      for (int i = 1; i <= n; i++)
        if (A[i] != B[i]) {
          stsum = A[i];
          A[i] = sum;
          fir = i;
          ans++;
          if (B[i] == sum) s.erase(s.lower_bound(pair<int, int>(B[i], i)));
          break;
        }
    while (!s.empty()) {
      it = s.lower_bound(pair<int, int>(stsum, 0));
      if ((*it).first != stsum) {
        it = s.lower_bound(pair<int, int>(sum, 0));
        if (it != s.end() && (*it).first == sum) {
          stsum = A[(*it).second];
          A[fir = (*it).second] = sum;
          ans++;
          s.erase(it);
        } else
          for (int i = 1; i <= n; i++)
            if (A[i] != B[i]) {
              stsum = A[i];
              A[i] = sum;
              fir = i;
              ans++;
              if (B[i] == sum) s.erase(s.lower_bound(pair<int, int>(B[i], i)));
              break;
            }
        continue;
      }
      if ((*it).second == fir) {
        itt = it;
        it++;
        if ((*it).first == stsum) {
          swap(stsum, A[(*it).second]);
          ans++;
          s.erase(it);
        } else {
          swap(stsum, A[(*itt).second]);
          ans++;
          s.erase(itt);
          if (s.empty()) break;
          fir = (*s.begin()).second;
          stsum = A[fir];
          A[fir] = sum;
          ans++;
          if (B[fir] == sum)
            s.erase(s.lower_bound(pair<int, int>(B[fir], fir)));
        }
      } else {
        swap(stsum, A[(*it).second]);
        ans++;
        s.erase(it);
      }
    }
    printf("%d\n", ans);
    return 0;
  }
  return 0;
}
