#include <bits/stdc++.h>
using namespace std;
const int N = 101010;
int n, a[N], b[N], ans, res, tot;
vector<int> va, vb, V[N];
set<int> all;
map<int, int> mp;
int main(void) {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), a[n + 1] ^= a[i];
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]), b[n + 1] ^= b[i];
  for (int i = 1; i <= n + 1; i++) {
    va.push_back(a[i]);
    vb.push_back(b[i]);
    res += (a[i] != b[i]) && (i <= n);
  }
  sort(va.begin(), va.end());
  sort(vb.begin(), vb.end());
  for (int i = 0; i <= n; i++) {
    if (va[i] != vb[i]) return puts("-1"), 0;
    if (mp.find(va[i]) == mp.end()) mp[va[i]] = ++tot;
  }
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) {
      all.insert(mp[b[i]]);
      V[mp[b[i]]].push_back(i);
    }
  int now = a[n + 1];
  while (res) {
    int id = mp[now];
    if (!V[id].size()) {
      int ind = V[*(all.begin())].back();
      swap(now, a[ind]);
    } else {
      res--;
      int ind = V[id].back();
      V[id].pop_back();
      if (a[ind] == a[n + 1] && V[id].size()) {
        int nind = V[id].back();
        V[id].pop_back();
        V[id].push_back(ind);
        ind = nind;
      }
      if (!V[id].size()) all.erase(id);
      swap(now, a[ind]);
    }
    ans++;
  }
  cout << ans << endl;
  return 0;
}
