#include <bits/stdc++.h>
using namespace std;
const int MAXN = 110100;
vector<int> v[MAXN];
int n, num, step, ans, a[MAXN], b[MAXN], c[MAXN * 2 + 100], diff, has[MAXN],
    cnt[MAXN], now[MAXN], anss, cur[MAXN];
map<int, int> mapp;
int find(int x, int st, int z) {
  if (cur[x] == 0) {
    if (x == st) return 1;
    if (z) return 1;
    return 0;
  }
  cur[x]--;
  if (!find(v[x][cur[x]], st, z)) return 0;
  cnt[x]--;
  return 1;
}
bool comp(const int &a, const int &b) { return a < b; }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    c[i] = a[i];
    num ^= a[i];
  }
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &b[i]);
    c[n + i] = b[i];
  }
  c[2 * n + 1] = num;
  sort(c + 1, c + 2 + 2 * n, comp);
  for (int i = 1; i <= 2 * n + 1; ++i)
    if (c[i] != c[i - 1])
      mapp[c[i]] = mapp[c[i - 1]] + 1;
    else
      mapp[c[i]] = mapp[c[i - 1]];
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i]) {
      ++ans;
      v[mapp[b[i]]].push_back(mapp[a[i]]);
      ++cnt[mapp[b[i]]];
    }
  for (int i = 1; i <= n; ++i) cur[mapp[b[i]]] = v[mapp[b[i]]].size();
  find(mapp[num], mapp[num], 1);
  for (int i = 1; i <= n; ++i) {
    while (cnt[mapp[b[i]]] && find(mapp[b[i]], mapp[b[i]], 0)) ++anss;
    if (cnt[mapp[b[i]]]) {
      printf("-1");
      return 0;
    }
  }
  printf("%d", ans + anss);
}
