#include <bits/stdc++.h>
using namespace std;
template <class BidirIt>
BidirIt prev(BidirIt it,
             typename iterator_traits<BidirIt>::difference_type n = 1) {
  advance(it, -n);
  return it;
}
template <class ForwardIt>
ForwardIt next(ForwardIt it,
               typename iterator_traits<ForwardIt>::difference_type n = 1) {
  advance(it, n);
  return it;
}
const double EPS = 1e-9;
const double PI = 3.141592653589793238462;
template <typename T>
inline T sq(T a) {
  return a * a;
}
const int MAXN = 1e5 + 5;
int a[MAXN], b[MAXN];
map<int, int> cnt;
map<int, vector<int> > need;
int main() {
  int n;
  scanf("%d", &n);
  int cur = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    cnt[a[i]]++;
    cur ^= a[i];
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    cnt[b[i]]--;
  }
  cnt[cur]++;
  int sum = 0;
  for (auto it : cnt) sum += abs(it.second);
  if (sum > 1) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] == b[i]) continue;
    need[b[i]].push_back(i);
  }
  int ans = 0;
  while (need.size()) {
    int pos;
    if (!need.count(cur)) {
      pos = need.begin()->second.back();
      need.begin()->second.pop_back();
      if (need.begin()->second.empty()) need.erase(need.begin());
    } else {
      pos = need[cur].back();
      need[cur].pop_back();
      if (need[cur].empty()) need.erase(cur);
    }
    swap(a[pos], cur);
    if (a[pos] != b[pos]) need[b[pos]].push_back(pos);
    ans++;
  }
  printf("%d\n", ans);
  return 0;
}
