#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
int a[N], b[N];
int x[N], y[N];
deque<int> q[N];
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int XR = 0, xr = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    xr ^= a[i];
    x[i] = a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    XR ^= b[i];
    y[i] = b[i];
  }
  y[n] = XR;
  x[n] = xr;
  sort(x, x + n + 1);
  sort(y, y + n + 1);
  for (int i = 0; i <= n; i++)
    if (x[i] != y[i]) {
      cout << "-1\n";
      return 0;
    }
  int cnt = unique(x, x + n + 1) - x;
  for (int i = 0; i < n; i++) a[i] = lower_bound(x, x + cnt, a[i]) - x;
  for (int i = 0; i < n; i++) b[i] = lower_bound(x, x + cnt, b[i]) - x;
  xr = lower_bound(x, x + cnt, xr) - x;
  for (int i = 0; i < n; i++)
    if (a[i] != b[i]) q[b[i]].push_back(i);
  int ans = 0;
  for (int i = 0; i < cnt; i++) {
    while (!q[xr].empty()) {
      int ind = q[xr].front();
      q[xr].pop_front();
      swap(a[ind], xr);
      ans++;
    }
    if (!q[i].empty()) {
      int ind = q[i].back();
      swap(a[ind], xr);
      ans++;
    }
  }
  cout << ans << "\n";
  return 0;
}
