#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
int a[maxn], b[maxn];
int vis[maxn];
map<int, int> mp;
vector<int> e[maxn];
void dfs(int now) {
  vis[now] = 1;
  for (auto p : e[now]) {
    if (!vis[p]) dfs(p);
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[0] ^= a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    b[0] ^= b[i];
  }
  for (int i = 0; i <= n; i++) {
    mp[a[i]]++;
    mp[b[i]]--;
  }
  for (auto p : mp) {
    if (p.second != 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  int id = 0;
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    if (!mp[a[i]]) mp[a[i]] = ++id;
  }
  for (int i = 0; i <= n; i++) {
    if (a[i] != b[i]) {
      e[mp[a[i]]].push_back(mp[b[i]]), ans++;
    }
  }
  for (int i = 1; i <= id; i++) {
    if (!vis[i] && !e[i].empty()) {
      ans++;
      dfs(id);
    }
  }
  if (a[0] != b[0]) ans--;
  if (!e[mp[a[0]]].empty()) ans--;
  cout << ans << endl;
  return 0;
}
