#include <bits/stdc++.h>
using namespace std;
const int NR = 1e5 + 100;
int n, a[NR], b[NR], sa, sb, c[NR], d[NR];
multimap<int, int> pa, pb;
int main() {
  scanf("%d", &n);
  int sa = 0, sb = 0;
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), sa ^= a[i], c[i] = a[i];
  for (int i = 1; i <= n; ++i) scanf("%d", &b[i]), sb ^= b[i], d[i] = b[i];
  c[n + 1] = sa;
  d[n + 1] = sb;
  sort(c + 1, c + n + 2);
  sort(d + 1, d + n + 2);
  for (int i = 1; i <= n + 1; ++i)
    if (c[i] != d[i]) {
      puts("-1");
      return 0;
    }
  for (int i = 1; i <= n; ++i) {
    if (a[i] != b[i]) pb.insert(pair<int, int>(b[i], i));
  }
  int ans = 0;
  while (true) {
    auto it = pb.find(sa);
    if (it == pb.end()) {
      if (sa != sb) {
        puts("-1");
        return 0;
      }
      if (pb.empty()) break;
      swap(a[pb.begin()->second], sa);
    } else {
      swap(a[it->second], sa);
      pb.erase(it);
    }
    ++ans;
  }
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i]) {
      puts("-1");
      return 0;
    }
  if (sa != sb) {
    puts("-1");
    return 0;
  }
  printf("%d\n", ans);
  return 0;
}
