#include <bits/stdc++.h>
using namespace std;
char ch;
int read() {
  int n = 0, p = 1;
  for (ch = getchar(); ch < '0' || '9' < ch; ch = getchar())
    if (ch == '-') p = -1;
  for (; '0' <= ch && ch <= '9'; ch = getchar()) n = n * 10 + ch - '0';
  return n * p;
}
const int N = 1e5 + 5;
int n, a[N], b[N];
map<int, int> ok, cb, fa, exi;
int getfa(int x) { return fa[x] ? fa[x] = getfa(fa[x]) : x; }
int main() {
  n = read();
  int x = 0;
  for (int i = 1; i <= n; i++) a[i] = read(), x ^= a[i];
  a[0] = x;
  x = 0;
  for (int i = 1; i <= n; i++) b[i] = read(), x ^= b[i];
  b[0] = x;
  for (int i = 0; i <= n; i++) ok[a[i]]++, cb[b[i]]++;
  bool same = 1;
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) same = 0;
  if (same) return puts("0"), 0;
  for (int i = 0; i <= n; i++)
    if (ok[a[i]] != cb[a[i]]) return puts("-1"), 0;
  int ans = 0;
  for (int i = 0; i <= n; i++)
    if (a[i] != b[i]) {
      exi[a[i]] = 1;
      if (i) ++ans;
      int f1 = getfa(a[i]), f2 = getfa(b[i]);
      if (f1 != f2) fa[f2] = f1;
    }
  for (int i = 0; i <= n; i++)
    if (exi[a[i]] && getfa(a[i]) == a[i]) ++ans;
  printf("%d", ans - 1);
  return 0;
}
