#include <bits/stdc++.h>
using namespace std;
inline int min(int a, int b) { return a < b ? a : b; }
inline int max(int a, int b) { return a > b ? a : b; }
int ch = 0;
template <class T>
inline void read(T &a) {
  bool f = 0;
  a = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    a = a * 10 + ch - '0';
    ch = getchar();
  }
  if (f) a = -a;
}
int n;
int a[100005], b[100005];
struct aa {
  int x, p;
  bool operator<(const aa &y) const { return x < y.x; }
};
multiset<int> s;
multiset<aa> ss;
int main() {
  read(n);
  {
    int sum = 0;
    for (int i = 1; i <= n; i++) read(a[i]), s.insert(a[i]), sum ^= a[i];
    s.insert(sum);
    sum = 0;
    for (int i = 1; i <= n; i++) {
      read(b[i]);
      sum ^= b[i];
      multiset<int>::iterator it = s.find(b[i]);
      if (it == s.end())
        return puts("-1"), 0;
      else
        s.erase(it);
    }
    multiset<int>::iterator it = s.find(sum);
    if (it == s.end()) return puts("-1"), 0;
  }
  int now = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] != b[i]) ss.insert((aa){b[i], i});
    now ^= a[i];
  }
  int ans = 0;
  multiset<aa>::iterator it;
  while ((it = ss.find((aa){now, 0})) != ss.end()) {
    it = ss.find((aa){now, 0});
    ans++;
    swap(now, a[it->p]);
    ss.erase(it);
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] != b[i]) {
      swap(a[i], now);
      ans++;
      while ((it = ss.find((aa){now, 0})) != ss.end()) {
        it = ss.find((aa){now, 0});
        ans++;
        swap(now, a[it->p]);
        ss.erase(it);
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
