#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, y;
  node(int _x, int _y) { x = _x, y = _y; }
};
bool operator<(node x, node y) {
  if (x.x == y.x) return x.y < y.y;
  return x.x < y.x;
}
set<node> s;
int n, a[120000], b[120000];
int x;
map<int, int> p;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), x ^= a[i], p[a[i]]++;
  p[x]++;
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]), p[b[i]]--;
  for (int i = 1; i <= n; i++)
    if (p[b[i]] < 0) {
      printf("-1\n");
      return 0;
    }
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) s.insert(node(b[i], a[i]));
  int ans = 0;
  while (!s.empty()) {
    node o = *s.lower_bound(node(x, -1));
    if (o.x != x) o = *s.begin();
    s.erase(o);
    swap(x, o.y);
    ans++;
    if (o.x != o.y) s.insert(o);
    if (ans > n * 5) {
      printf("-1\n");
      return 0;
    }
  }
  printf("%d\n", ans);
}
