#include <bits/stdc++.h>
static const int MAXN = 1e5 + 4;
int n;
int a[MAXN], b[MAXN];
inline int min_rots(int n, int *a, int *b) {
  std::map<int, std::vector<int>> p;
  for (int i = 0; i < n; ++i) p[a[i]].push_back(i);
  int ans = 1;
  for (int i = 0; i < n; ++i)
    if (b[i] != a[i]) ++ans;
  return (ans == 1 ? 0 : ans);
}
int main() {
  scanf("%d", &n);
  int xor_sum = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    xor_sum ^= a[i];
  }
  for (int i = 0; i < n; ++i) scanf("%d", &b[i]);
  std::multiset<int> as;
  for (int i = 0; i < n; ++i) as.insert(a[i]);
  bool has_sum = false;
  for (int i = 0; i < n; ++i) {
    auto p = as.find(b[i]);
    if (p == as.end()) {
      if (b[i] == xor_sum && !has_sum) {
        has_sum = true;
      } else {
        puts("-1");
        return 0;
      }
    } else {
      as.erase(p);
    }
  }
  if (has_sum) {
    int idx = -1;
    for (int i = 0; i < n; ++i)
      if (b[i] == xor_sum && a[i] != b[i]) {
        idx = i;
        break;
      }
    if (idx == -1)
      for (int i = 0; i < n; ++i)
        if (b[i] == xor_sum) {
          idx = i;
          break;
        }
    a[idx] = b[idx];
    printf("%d\n", min_rots(n, a, b));
  } else {
    printf("%d\n", min_rots(n, a, b));
  }
  return 0;
}
