#include <bits/stdc++.h>
using namespace std;
const long long N = 12e4;
int a[N], b[N], aa[N], bb[N];
int n, m, hs;
int fa[N];
map<int, int> s;
inline int get(int o) { return (fa[o] == o) ? o : (fa[o] = get(fa[o])); }
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), a[n + 1] ^= a[i];
  for (int i = 1; i <= n; ++i) scanf("%d", &b[i]), b[n + 1] ^= b[i];
  ++n;
  for (int i = 1; i <= n; ++i) aa[i] = a[i], bb[i] = b[i];
  sort(aa + 1, aa + n + 1);
  sort(bb + 1, bb + n + 1);
  for (int i = 1; i <= n; ++i)
    if (aa[i] != bb[i]) {
      puts("-1");
      return 0;
    }
  int p = 0;
  for (int i = 1; i <= n; ++i)
    if (aa[i] != aa[i - 1]) s[aa[i]] = ++p;
  int ans = -1;
  for (int i = 1; i <= n; ++i) a[i] = s[a[i]];
  for (int i = 1; i <= n; ++i) b[i] = s[b[i]];
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i]) fa[a[i]] = a[i], fa[b[i]] = b[i], ans = ans + (i < n);
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i]) fa[get(a[i])] = get(b[i]);
  for (int i = 1; i <= p; ++i)
    if (!fa[i]) fa[i] = get(i);
  for (int i = 1; i <= p; ++i)
    if (fa[i] == i) ++hs;
  if (ans < 0) ans = 0;
  ans = ans + hs;
  cout << ans;
}
