#include <bits/stdc++.h>
using namespace std;
long long M = 1e9 + 7;
const int N = 2e5 + 7;
map<int, int> mp;
vector<int> ad[N];
int dif[N], m;
int n, a[N], b[N];
int com;
bool mrk[N];
void dfs(int v) {
  mrk[v] = 1;
  for (auto u : ad[v])
    if (!mrk[u]) dfs(u);
}
int cnt[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  int x = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    x ^= a[i];
  }
  int res = 0;
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    if (!mp[b[i]]) {
      m++;
      mp[b[i]] = m;
    }
    if (!mp[a[i]]) {
      m++;
      mp[a[i]] = m;
    }
    a[i] = mp[a[i]];
    b[i] = mp[b[i]];
    cnt[a[i]]++;
    cnt[b[i]]--;
    if (a[i] != b[i]) {
      res++;
      dif[b[i]]++;
      dif[a[i]]--;
      ad[b[i]].push_back(a[i]);
      ad[a[i]].push_back(b[i]);
    }
  }
  if (res == 0) {
    cout << 0;
    return 0;
  }
  if (!mp[x]) {
    m++;
    mp[x] = m;
  }
  x = mp[x];
  cnt[x]++;
  int d = 0;
  for (int i = 1; i <= m; i++) {
    if (dif[i] > 0) d = 1;
    if (cnt[i] < 0) {
      cout << -1;
      return 0;
    }
    if (!mrk[i] && ad[i].size()) {
      dfs(i);
      com++;
    }
  }
  if (!d)
    cout << res + com - 1;
  else {
    if (dif[x] > 0)
      cout << res + com - 1;
    else
      cout << res + com;
  }
}
