#include <bits/stdc++.h>
using namespace ::std;
const long long maxn = 1e5 + 500;
const long long inf = 1e9;
long long a[maxn];
long long b[maxn];
multiset<long long> st;
map<long long, vector<long long> > ger;
map<long long, bool> vis;
void dfs(long long a) {
  vis[a] = 1;
  for (long long i = 0; i < (long long)ger[a].size(); i++) {
    long long v = ger[a][i];
    if (vis[v] == 0) {
      dfs(v);
    }
  }
}
int main() {
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    a[n] ^= a[i];
    st.insert(a[i]);
  }
  st.insert(a[n]);
  for (long long i = 0; i < n; i++) {
    cin >> b[i];
    if (st.find(b[i]) == st.end()) {
      cout << -1;
      return 0;
    }
    st.erase(st.find(b[i]));
  }
  b[n] = (*st.begin());
  long long ans = -2;
  for (long long i = 0; i <= n; i++) {
    if (a[i] != b[i]) {
      ger[a[i]].push_back(b[i]);
      ger[b[i]].push_back(a[i]);
    }
    ans += (a[i] != b[i]);
  }
  for (long long i = 0; i <= n; i++) {
    if (a[i] != b[i] && vis[a[i]] == 0) {
      dfs(a[i]);
      ans++;
    }
  }
  if (b[n] == a[n]) ans++;
  ans = max(ans, (long long)0);
  cout << ans;
}
