#include <bits/stdc++.h>
const int N = (int)1e5 + 228;
using namespace std;
map<int, int> cnt, pos;
int n, a[N], b[N], m, cur, all = 0, e, ans;
vector<int> comp, g[N * 2], ord;
bool was[N * 2], found;
void dfs(int v) {
  was[v] = true;
  for (auto to : g[v]) {
    if (!was[to]) dfs(to);
  }
  ord.push_back(v);
}
void dfs1(int v) {
  if (v <= n) {
    all++;
    if (b[v] == e) found = true;
  }
  was[v] = true;
  for (auto to : g[v]) {
    if (!was[to]) dfs1(to);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  int res = 0;
  for (int i = 1; i <= n; i++) cnt[b[i]]++;
  for (int i = 1; i <= n; i++) {
    if (cnt[a[i]] == 0) m++;
    if (a[i] != b[i]) res++;
  }
  if (m > 1) {
    cout << -1 << endl;
    return 0;
  }
  if (m == 1) {
    int x, y = 0;
    for (int i = 1; i <= n; i++) {
      y ^= a[i];
      if (cnt[b[i]] > 0) x = i;
    }
    if (y == x) {
      cout << res << endl;
    } else {
      cout << -1 << endl;
    }
    return 0;
  }
  if (m == 0) {
    cur = n;
    for (int i = 1; i <= n; i++)
      if (a[i] != b[i]) comp.push_back(a[i]);
    sort(comp.begin(), comp.end());
    for (int i = 0; i < (int)comp.size(); i++) {
      if (i == 0 || comp[i] != comp[i - 1]) cur++;
      pos[comp[i]] = cur;
    }
    for (int i = 1; i <= n; i++) {
      if (a[i] != b[i]) {
        g[pos[b[i]]].push_back(i);
        g[i].push_back(pos[a[i]]);
      }
    }
    for (int i = 1; i <= n; i++) e ^= a[i];
    for (int i = 1; i <= cur; i++)
      if (!was[i] && b[i] != a[i]) dfs(i);
    for (int i = 1; i <= cur; i++) was[i] = false;
    for (auto v : ord) {
      if (!was[v]) {
        all = 0, found = false;
        dfs1(v);
        ans += all + !found;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
