#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<long long, int> ok, cb;
int main() {
  n = read();
  int x = 0;
  for (int i = 1; i <= n; i++) a[i] = read(), x ^= a[i], ++ok[a[i]];
  for (int i = 1; i <= n; i++) ++cb[b[i] = read()];
  bool same = 1;
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) same = 0;
  if (same) return puts("0"), 0;
  int diff = 0;
  for (int i = 1; i <= n; i++) {
    if (!ok[b[i]] && b[i] != x) return puts("-1"), 0;
    diff += abs(ok[a[i]] - cb[a[i]]);
  }
  if (diff > 1) return puts("-1"), 0;
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) ++cnt;
  if (ok[x] < cb[x]) return printf("%d", cnt), 0;
  if (!ok[x]) return printf("%d", cnt + 2), 0;
  for (int i = 1; i <= n; i++)
    if (b[i] == x && a[i] != x) return printf("%d", cnt + 1), 0;
  printf("%d", cnt + 2);
  return 0;
}
