#include <bits/stdc++.h>
using namespace std;
map<int, queue<int> > to;
int N;
int a[100005];
int b[100005];
int vis[100005];
map<int, int> ca, cb;
void No() {
  cout << "-1" << endl;
  exit(0);
}
int main() {
  std::ios::sync_with_stdio(false);
  cin >> N;
  for (int i = 0; i < N; i++) cin >> a[i];
  ;
  for (int i = 0; i < N; i++) cin >> b[i];
  ;
  for (int i = 0; i < N; i++) {
    ca[a[i]]++;
    cb[b[i]]++;
  }
  int res = 0;
  int kagi = 0;
  for (int i = 0; i < N; i++) kagi ^= a[i];
  for (int i = 0; i < N; i++) {
    if (a[i] == b[i]) continue;
    to[b[i]].push(i);
  }
  bool mul = 0;
  for (int i = 0; i < N; i++) {
    if (a[i] != b[i] && b[i] == kagi) {
      mul = 1;
      int p = i;
      while (!vis[p]) {
        vis[p] = 1;
        res++;
        if (to[a[p]].size() == 0) break;
        int tmp = to[a[p]].front();
        to[a[p]].pop();
        p = tmp;
      }
      kagi = a[p];
      break;
    }
  }
  bool gagal = 0;
  for (int i = 0; i < N; i++) {
    if (!vis[i] && a[i] != b[i]) gagal = 1;
  }
  if (!gagal) {
    cout << res << endl;
    return 0;
  }
  if (to[kagi].size() == 0) res++;
  res += mul;
  for (int i = 0; i < N; i++) {
    if (a[i] == b[i]) continue;
    if (vis[i]) continue;
    int p = i;
    while (!vis[p]) {
      vis[p] = 1;
      res++;
      if (to[a[p]].size() == 0) No();
      int tmp = to[a[p]].front();
      to[a[p]].pop();
      p = tmp;
    }
    res++;
  }
  cout << res - 1 << endl;
}
