#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') f = -1;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  return x * f;
}
const int N = 300005;
long long a[N], b[N];
map<long long, int> cnt;
multiset<pair<long long, int> > s;
multiset<pair<long long, int> >::iterator it;
int main() {
  int n = read(), ans = 0;
  long long sum = 0, now;
  for (int i = 1; i <= n; ++i) a[i] = read(), cnt[a[i]]++, sum ^= a[i];
  cnt[sum]++;
  for (int i = 1; i <= n; ++i) b[i] = read();
  for (int i = 1; i <= n; ++i)
    if (cnt[b[i]] == 0) {
      puts("-1");
      return 0;
    } else
      cnt[b[i]]--;
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i]) s.insert(make_pair(b[i], i));
  now = sum;
  while (!s.empty()) {
    it = s.lower_bound(make_pair(now, 0));
    if (it == s.end() || ((*it).first) != now) {
      it = s.begin();
      ans++;
      now = (*it).first;
    }
    ans++;
    now = a[(*it).second];
    s.erase(it);
  }
  cout << ans;
  return 0;
}
