#include <bits/stdc++.h>
using namespace std;
int par[200009];
inline int parent(int pos) {
  if (par[pos] != pos) {
    par[pos] = parent(par[pos]);
  }
  return par[pos];
}
set<int> s;
map<int, int> mp1, mp2;
int cnt = 0;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  long long x = 0;
  int a[n + 1], b[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    x = x ^ a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }
  if (n == 1) {
    if (a[1] != b[1]) {
      cout << -1 << "\n";
      return 0;
    }
  }
  vector<int> v1, v2;
  for (int i = 1; i <= n; i++) {
    if (a[i] == b[i]) {
    } else {
      v1.push_back(a[i]);
      v2.push_back(b[i]);
    }
  }
  if (v1.empty()) {
    cout << 0 << "\n";
    return 0;
  }
  for (int i = 0; i < v1.size(); i++) {
    mp1[v1[i]]++;
    mp2[v2[i]]++;
  }
  map<int, int>::iterator it1;
  for (it1 = mp1.begin(); it1 != mp1.end(); it1++) {
    if (mp1[it1->first] == mp2[it1->first]) {
      continue;
    } else {
      if (abs(mp1[it1->first] - mp2[it1->first]) > 1) {
        cout << -1 << "\n";
        return 0;
      } else {
        if (mp1[it1->first] - mp2[it1->first] == 1) {
          cnt++;
          continue;
        } else {
          cout << -1 << "\n";
          return 0;
        }
      }
    }
  }
  if (cnt == 1) {
    if (mp2[x] - mp1[x] == 1) {
    } else {
      cout << -1 << "\n";
      return 0;
    }
  } else if (cnt > 1) {
    cout << -1 << "\n";
    return 0;
  }
  bool flag = false;
  for (int i = 0; i < v2.size(); i++) {
    if (v2[i] == x) {
      flag = true;
      break;
    }
  }
  vector<pair<int, int> > v;
  for (int i = 1; i <= v1.size(); i++) {
    par[i] = i;
  }
  for (int i = 0; i < v1.size(); i++) {
    v.push_back(make_pair(v1[i], i + 1));
    v.push_back(make_pair(v2[i], i + 1));
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size() - 1; i++) {
    if (v[i].first == v[i + 1].first) {
      int p1 = parent(v[i].second);
      int p2 = parent(v[i + 1].second);
      if (p1 != p2) {
        par[max(p1, p2)] = min(p1, p2);
      }
    }
  }
  set<int> s;
  for (int i = 1; i <= v1.size(); i++) {
    s.insert(par[i]);
  }
  int ans = (int)s.size();
  ans += v1.size();
  if (flag) {
    ans--;
    ans--;
  }
  cout << ans << "\n";
}
