#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int a[MAXN], b[MAXN], n;
int dx[MAXN * 2], dx_top = 0;
int T[MAXN * 2];
stack<int> stk[MAXN * 2];
queue<int> que;
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  int cur = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    dx[++dx_top] = a[i];
    cur ^= a[i];
  }
  dx[++dx_top] = cur;
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    dx[++dx_top] = b[i];
  }
  sort(dx + 1, dx + dx_top + 1);
  dx_top = unique(dx + 1, dx + dx_top + 1) - dx - 1;
  cur = lower_bound(dx + 1, dx + dx_top + 1, cur) - dx;
  T[cur]++;
  for (int i = 1; i <= n; i++) {
    a[i] = lower_bound(dx + 1, dx + dx_top + 1, a[i]) - dx;
    b[i] = lower_bound(dx + 1, dx + dx_top + 1, b[i]) - dx;
    T[b[i]]--;
    T[a[i]]++;
  }
  int cnt = 0;
  for (int i = 1; i <= dx_top; i++) {
    if (T[i] != 0 && T[i] != 1) {
      cout << -1 << endl;
      return 0;
    }
    cnt += T[i];
  }
  if (cnt > 1) {
    cout << -1 << endl;
    return 0;
  }
  throw;
  for (int i = 1; i <= n; i++) stk[b[i]].push(i);
  int ma = 0;
  for (int i = 1; i <= dx_top; i++)
    if (!stk[i].empty()) que.push(i);
  cnt = 0;
  while (ma < n) {
    auto reduce = [&](int cur) {
      int now = -1;
      while (!stk[cur].empty()) {
        now = stk[cur].top();
        if (a[now] == b[now])
          stk[cur].pop(), ma++;
        else
          break;
      }
    };
    reduce(cur);
    if (!stk[cur].empty()) {
      int now = stk[cur].top();
      stk[cur].pop();
      swap(cur, a[now]), ma++, cnt++;
    } else {
      int j = -1;
      while (!que.empty()) {
        int now = que.front();
        reduce(now);
        if (!stk[now].empty()) {
          j = now;
          break;
        } else
          que.pop();
      }
      if (j == -1) {
        assert(ma == n);
        break;
      }
      int now = stk[j].top();
      swap(cur, a[now]), cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}
