#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
const long long INF = 1e18;
const long long MAXN = 100100;
int N, A[MAXN];
int B[MAXN], K;
multiset<long long> M;
int ans;
map<int, int> P;
int par(int x) { return (x == P[x]) ? x : P[x] = par(P[x]); }
int main() {
  cin >> N;
  for (int i = 1; i <= N; ++i) {
    cin >> A[i];
  }
  for (int i = 1; i <= N; ++i) {
    cin >> B[i];
    K ^= A[i];
    if (A[i] == B[i]) continue;
    M.insert(B[i]);
    ++ans;
  }
  long long t = (long long)M.size();
  if (M.find(K) != M.end()) {
    M.erase(M.find(K));
    ans--;
  }
  for (int i = 1; i <= N; ++i) {
    if (M.find(A[i]) != M.end()) {
      M.erase(M.find(A[i]));
    }
  }
  if ((long long)M.size()) {
    cout << -1;
    return 0;
  }
  for (int i = 1; i <= N; ++i)
    if (A[i] != B[i]) {
      P[B[i] + 1] = B[i] + 1;
    }
  for (int i = 1; i <= N; ++i)
    if (A[i] != B[i]) {
      if (P[A[i] + 1] == 0) continue;
      int a = par(A[i] + 1);
      int b = par(B[i] + 1);
      P[a] = b;
    }
  int extra = 0;
  for (auto i : P)
    if (i.first == i.second && i.second) ++extra;
  cout << ans + extra;
}
