#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const int maxn = 100010;
int n, x, ans, tot, a[maxn], b[maxn], fa[maxn];
multiset<int> S;
map<int, int> mp;
bool used[maxn];
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
int id(int x) { return mp.count(x) ? mp[x] : mp[x] = ++tot; }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]), x ^= a[i], S.insert(a[i]);
  }
  S.insert(x), fa[n + 1] = n + 1;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]), fa[i] = i;
    if (S.find(b[i]) == S.end()) printf("-1\n"), exit(0);
    S.erase(S.find(b[i]));
  }
  for (int i = 1; i <= n; i++)
    if (a[i] ^ b[i]) {
      int u = id(a[i]), v = id(b[i]);
      fa[find(u)] = find(v), ans++;
    }
  if (!ans) printf("0\n"), exit(0);
  for (int i = 1; i <= tot; i++) {
    ans += (find(i) == i);
  }
  for (int i = 1; i <= n; i++) {
    if (b[i] == x) ans--;
  }
  printf("%d\n", ans);
  return 0;
}
