#include <bits/stdc++.h>
using namespace std;
const int N = 200000 + 10;
int n, a[N], b[N];
vector<int> v;
int id(int x) { return lower_bound(v.begin(), v.end(), x) - v.begin() + 1; }
int par[N], sz[N];
int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }
int main() {
  for (int i = 0; i < N; i++) par[i] = i, sz[i] = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    a[0] ^= a[i];
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    b[0] ^= b[i];
  }
  set<int> s1, s2;
  for (int i = 0; i <= n; i++) {
    s1.insert(a[i]);
    s2.insert(b[i]);
    v.push_back(a[i]);
  }
  if (s1 != s2) return !printf("-1\n");
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  for (int i = 0; i <= n; i++) {
    if (find(id(a[i])) != find(id(b[i]))) {
      par[find(id(a[i]))] = find(id(b[i]));
    }
  }
  for (int i = 0; i <= n; i++) {
    if (a[i] != b[i]) {
      sz[find(id(a[i]))]++;
    }
  }
  int ans = 0;
  for (int i = 1; i <= v.size(); i++) {
    if (i != find(i) || sz[find(i)] == 0) continue;
    if (find(i) != find(id(a[0]))) {
      ans += sz[find(i)] + 1;
    } else {
      ans += sz[find(i)] - 1;
    }
  }
  if (a[0] == b[0]) ans++;
  cout << ans << endl;
}
