#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000;
int n;
int a[MAX_N + 1];
int b[MAX_N + 1];
namespace union_find {
unordered_map<int, int> parent;
unordered_map<int, int> rank;
void init() {
  for (int i = 0; i <= n; i++) {
    parent[a[i]] = a[i];
    rank[a[i]] = 0;
  }
}
int find(int a) {
  if (parent[a] == a) return a;
  return parent[a] = find(parent[a]);
}
void merge(int a, int b) {
  a = find(a);
  b = find(b);
  if (a == b) return;
  if (rank[a] < rank[b]) {
    parent[a] = b;
  } else {
    parent[b] = a;
    if (rank[a] == rank[b]) rank[a]++;
  }
}
}  // namespace union_find
int main() {
  cin >> n;
  a[0] = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    a[0] ^= a[i];
  }
  b[0] = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> b[i];
    b[0] ^= b[i];
  }
  unordered_map<int, int> ma, mb;
  for (int i = 0; i <= n; i++) {
    ma[a[i]]++;
    mb[b[i]]++;
  }
  if (ma != mb) {
    cout << -1 << endl;
    return 0;
  }
  union_find::init();
  int e = 0;
  for (int i = 1; i <= n; i++) {
    union_find::merge(a[i], b[i]);
    if (a[i] != b[i]) {
      e++;
    }
  }
  unordered_map<int, int> ctr;
  for (int i = 0; i <= n; i++) {
    ctr[union_find::find(a[i])]++;
  }
  int k = ctr.size();
  int c = 0;
  for (auto p : ctr) {
    if (p.second == 1) c++;
  }
  if (k - c <= 1)
    cout << e << endl;
  else
    cout << k + e - 1 << endl;
  return 0;
}
