#include <bits/stdc++.h>
#pragma GCC optimize("3", "inline", "Ofast")
using namespace std;
int qreadInt() {
  int x = 0, op = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') op = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') (x *= 10) += (ch - '0'), ch = getchar();
  return x * op;
}
void cmin(int &x, int y) { x = (x > y) ? y : x; }
int a[100010], b[100010], sum[100010], vis[100010];
vector<int> vec[100010];
map<int, int> Map;
int main() {
  int n = qreadInt(), now = 0, Ans = 0, tot = 0;
  for (int i = (1); i <= (n); ++i) a[i] = qreadInt(), now ^= a[i];
  for (int i = (1); i <= (n); ++i) b[i] = qreadInt();
  for (int i = (1); i <= (n); ++i) Map[a[i]] = 1;
  for (int i = (1); i <= (n); ++i) Map[b[i]] = 1;
  Map[now] = 1;
  int tt = 0;
  for (map<int, int>::iterator it = Map.begin(); it != Map.end(); ++it, ++tt)
    Map[it->first] = tt;
  for (int i = (1); i <= (n); ++i) a[i] = Map[a[i]];
  for (int i = (1); i <= (n); ++i) b[i] = Map[b[i]];
  now = Map[now];
  for (int i = (1); i <= (n); ++i)
    if (a[i] != b[i])
      vec[b[i]].push_back(i);
    else
      vis[i] = 1, tot++;
  int tmp = now;
  for (int i = (1); i <= (n); ++i)
    if (!vis[i]) {
      int res = 0;
      int temp = i;
      sum[b[i]]++;
      while (1) {
        if (vis[temp]) {
          Ans -= res;
          break;
        }
        if (tmp == b[temp]) res = 1;
        swap(now, a[temp]), Ans++;
        if (a[temp] == b[temp]) vis[temp] = 1, tot++;
        if (tot == n) {
          return 0 * printf("%d\n", Ans);
        }
        if (vec[now].empty() || vec[now].size() == sum[now]) {
          if (now != b[i]) return 0 * puts("-1");
          swap(a[i], now), Ans++;
          Ans -= res;
          break;
        }
        sum[now]++;
        temp = vec[now][sum[now] - 1];
      }
    }
  cout << Ans << endl;
  return 0;
}
