#include <bits/stdc++.h>
using namespace std;
template <class T>
int getbit(T s, int i) {
  return (s >> i) & 1;
}
template <class T>
T onbit(T s, int i) {
  return s | (T(1) << i);
}
template <class T>
T offbit(T s, int i) {
  return s & (~(T(1) << i));
}
template <class T>
int cntbit(T s) {
  return __builtin_popcnt(s);
}
template <class T>
T gcd(T a, T b) {
  T r;
  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
template <class T>
T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
int n, a[500005], b[500005], numa[500005], numb[500005];
vector<int> V[500005];
vector<int> VV;
bool flag[500005];
int que[500005], size;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  int XO = 0, xo;
  for (int i = (1); i <= (n); ++i) {
    cin >> a[i];
    XO ^= a[i];
    VV.push_back(a[i]);
  }
  for (int i = (1); i <= (n); ++i) {
    cin >> b[i];
    VV.push_back(b[i]);
  }
  VV.push_back(XO);
  sort((VV).begin(), (VV).end());
  memset(numa, 0, sizeof(numa));
  memset(numb, 0, sizeof(numb));
  for (int i = (1); i <= (n); ++i) {
    int ta = lower_bound((VV).begin(), (VV).end(), a[i]) - VV.begin() + 1;
    int tb = lower_bound((VV).begin(), (VV).end(), b[i]) - VV.begin() + 1;
    if (ta != tb) {
      numa[ta]++;
      numb[tb]++;
      V[ta].push_back(tb);
      V[tb].push_back(ta);
    }
  }
  xo = lower_bound((VV).begin(), (VV).end(), XO) - VV.begin() + 1;
  memset(flag, 0, sizeof(flag));
  long long res = 0;
  bool ok = true;
  for (int i = (1); i <= (n + n + 2); ++i)
    if (((long long)(V[i]).size()) && !flag[i]) {
      size = 0;
      que[size++] = i;
      flag[i] = 1;
      int numedge = 0;
      bool havexo = 0;
      for (int t = 0; t < (size); ++t) {
        int u = que[t];
        if (u != xo && numb[u] != numa[u]) {
          ok = false;
        }
        if (u == xo && (numb[u] < numa[u] || numb[u] > numa[u] + 1)) ok = false;
        if (u == xo) havexo = 1;
        for (int j = 0; j < (((long long)(V[u]).size())); ++j) {
          numedge++;
          int v = V[u][j];
          if (!flag[v]) {
            flag[v] = 1;
            que[size++] = v;
          }
        }
      }
      if (havexo)
        res += numedge / 2;
      else
        res += numedge / 2 + 1;
    }
  if (!ok) {
    cout << -1 << endl;
  } else
    cout << res << endl;
  return 0;
}
