#include <bits/stdc++.h>
using namespace std;
int n, curr, res, comp;
int a[100007];
int b[100007];
bitset<100007> vis;
map<int, int> A;
map<int, int> B;
map<int, vector<int> > pos;
void dfs(int v) {
  vis[v] = 1;
  while (!pos[a[v]].empty() && vis[pos[a[v]].back()]) pos[a[v]].pop_back();
  if (!pos[a[v]].empty()) dfs(pos[a[v]].back());
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    curr ^= a[i];
    ++A[a[i]];
  }
  for (int i = 1; i <= n; ++i) {
    cin >> b[i];
    ++B[b[i]];
  }
  if (A != B) {
    for (int i = 1; i <= n; ++i) {
      if (A[a[i]] > B[a[i]]) {
        --A[a[i]];
        ++A[curr];
        a[i] = curr;
        if (a[i] == b[i]) ++res;
        break;
      }
    }
  }
  if (A != B) {
    cout << -1;
    return 0;
  }
  for (int i = 1; i <= n; ++i) {
    if (a[i] != b[i]) {
      ++res;
      pos[b[i]].push_back(i);
    } else
      vis[i] = 1;
  }
  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) {
      ++comp;
      dfs(i);
    }
  }
  cout << (comp == 0 ? res : res + comp - 1);
}
