#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
inline long long in() {
  long long x = 0, flag = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') flag = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x * flag;
}
int n, m, ans;
long long xsum, ori[MAXN], tar[MAXN], tot[MAXN];
int a[MAXN], b[MAXN];
bool vis[MAXN];
map<long long, int> cnt;
map<long long, int> id;
vector<int> pos[MAXN];
bool cmp(int x, int y) { return ori[x] < ori[y]; }
bool judge() {
  for (int i = 1; i <= n; i++) cnt[ori[i]] = cnt[ori[i]] + 1;
  cnt[xsum] = cnt[xsum] + 1;
  for (int i = 1; i <= n; i++) {
    if (cnt[tar[i]] > 0)
      cnt[tar[i]]--;
    else
      return false;
  }
  return true;
}
void init() {
  sort(tot + 1, tot + n + 2);
  id[tot[1]] = 1;
  for (int i = 2, kind = 1; i <= n + 1; i++) {
    if (tot[i] != tot[i - 1]) kind++;
    id[tot[i]] = kind;
  }
  for (int i = 1; i <= n; i++)
    if (cnt[ori[i]] != 0) {
      swap(ori[i], xsum);
      ans = -1;
      break;
    }
  for (int i = 1; i <= n; i++) {
    a[i] = id[ori[i]];
    b[i] = id[tar[i]];
    pos[b[i]].push_back(i);
  }
}
void DFS(int u) {
  ans++;
  vis[u] = true;
  for (int i = 0; i < pos[a[u]].size(); i++) {
    int v = pos[a[u]][i];
    if (vis[v] == true) continue;
    DFS(v);
  }
}
int main() {
  n = in();
  for (int i = 1; i <= n; i++) {
    tot[i] = ori[i] = in();
    xsum ^= ori[i];
  }
  tot[n + 1] = xsum;
  for (int i = 1; i <= n; i++) {
    tar[i] = in();
  }
  if (judge() == false) {
    printf("-1");
    return 0;
  }
  init();
  for (int i = 1; i <= n; i++)
    if (a[i] == b[i]) {
      vis[i] = true;
    }
  for (int i = 1; i <= n; i++) {
    if (vis[i]) continue;
    DFS(i);
    ans++;
  }
  cout << ans << endl;
  return 0;
}
