#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int INF = 1000 * 1000 * 1000 + 7;
const long long LINF = INF * (long long)INF;
const int MAX = 1000 * 100 + 47;
const int MOD = 1000 * 1000 + 3;
int A[MAX];
int B[MAX];
int A1[MAX];
int B1[MAX];
int L[MAX * 2];
int sz;
vector<int> g[MAX];
int IN[MAX];
int OUT[MAX];
int U[MAX];
int val = 0;
int x = 0;
bool wasX = false;
void dfs(int v) {
  if (U[v]) return;
  U[v] = 1;
  if (v == x) wasX = true;
  if (OUT[v] > IN[v]) val += OUT[v] - IN[v];
  for (int i = (0); i < ((int)g[v].size()); i++) {
    int to = g[v][i];
    dfs(to);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = (0); i < (n); i++) cin >> A[i];
  for (int i = (0); i < (n); i++) cin >> B[i];
  int y = 0;
  for (int i = (0); i < (n); i++) {
    x ^= A[i];
    y ^= B[i];
    A1[i] = A[i];
    B1[i] = B[i];
  }
  A1[n] = x;
  B1[n] = y;
  sort(A1, A1 + n + 1);
  sort(B1, B1 + n + 1);
  bool ok = true;
  for (int i = (0); i < (n + 1); i++) {
    if (A1[i] != B1[i]) {
      ok = false;
      break;
    }
  }
  if (!ok) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = (0); i < (n); i++) {
    L[sz++] = A[i];
    L[sz++] = B[i];
  }
  sort(L, L + sz);
  sz = unique(L, L + sz) - L;
  for (int i = (0); i < (n); i++) {
    A[i] = lower_bound(L, L + sz, A[i]) - L;
    B[i] = lower_bound(L, L + sz, B[i]) - L;
  }
  x = lower_bound(L, L + sz, x) - L;
  int ans = 0;
  for (int i = (0); i < (n); i++) {
    int a = A[i], b = B[i];
    if (a == b) continue;
    ans++;
    g[a].push_back(b);
    g[b].push_back(a);
    OUT[b]++;
    IN[a]++;
  }
  for (int i = (0); i < (sz); i++) {
    if (U[i] || (int)g[i].size() == 0) continue;
    wasX = false;
    val = 0;
    dfs(i);
    if (val || wasX) {
      if (wasX) {
        if (OUT[x] > IN[x]) val--;
      }
      ans += val;
      continue;
    }
    ans++;
  }
  cout << ans << endl;
}
