#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, a[N], m, b[N], k, cnt, fa[N], ans, now;
bool vis[N];
map<int, int> D, E;
inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
inline void link(int u, int v) {
  vis[u] = vis[v] = true;
  int fu = find(u), fv = find(v);
  if (fu != fv) fa[fu] = fv;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), m ^= a[i], D[a[i]]++;
  D[m]++;
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]), k ^= b[i], D[b[i]]--;
  n++;
  a[n] = m;
  b[n] = k;
  for (auto& i : D)
    if (i.second < 0) puts("-1"), exit(0);
  for (int i = 1; i <= n; i++) E.insert({a[i], 0}), fa[i] = i;
  for (auto& i : E) i.second = ++cnt;
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) ans++, link(E[a[i]], E[b[i]]);
  if (ans > 0) {
    ans--;
    for (int i = 1; i <= n; i++)
      if (vis[i]) {
        if (!now)
          now = i;
        else if (find(i) != find(now)) {
          ans++;
          break;
        }
      }
  }
  printf("%d\n", ans);
  return 0;
}
