#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmin(T& a, const T& b) {
  return a > b ? (a = b, 1) : 0;
}
template <class T>
bool chmax(T& a, const T& b) {
  return a < b ? (a = b, 1) : 0;
}
template <class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T>
void pv(T a, T b) {
  for (T i = a; i != b; ++i) cout << (*i) << " ";
  cout << endl;
}
int nextInt() {
  int x;
  scanf("%d", &x);
  return x;
}
struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool link(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
  bool same(int x, int y) { return root(x) == root(y); }
  int num() {
    int res = 0;
    for (int i = 0, _n = (int)(data.size()); i < _n; ++i) res += root(i) == i;
    return res;
  }
};
vector<int> normalize(vector<int> v) {
  const int N = v.size();
  vector<pair<int, int> > t(N);
  for (int i = 0, _n = (int)(N); i < _n; ++i) {
    t[i].first = v[i];
    t[i].second = i;
  }
  sort((t).begin(), (t).end());
  int r = 0;
  for (int i = 0, _n = (int)(N); i < _n; ++i) {
    r += (i > 0 && t[i - 1].first != t[i].first);
    v[t[i].second] = r;
  }
  return v;
}
int exor_all(const vector<int>& v) {
  int res = 0;
  for (int i = 0, _n = (int)(v.size()); i < _n; ++i) res ^= v[i];
  return res;
}
int main2() {
  int N = nextInt();
  vector<int> a(N), b(N);
  for (int i = 0, _n = (int)(N); i < _n; ++i) a[i] = nextInt();
  for (int i = 0, _n = (int)(N); i < _n; ++i) b[i] = nextInt();
  int xa = exor_all(a);
  int xb = exor_all(b);
  {
    vector<int> ta = a;
    vector<int> tb = b;
    ta.push_back(xa);
    tb.push_back(xb);
    sort(ta.begin(), ta.end());
    sort(tb.begin(), tb.end());
    if (ta != tb) {
      cout << -1 << endl;
      return 0;
    }
  }
  bool iso = true;
  for (int i = 0, _n = (int)(N); i < _n; ++i)
    if (a[i] == xa) iso = false;
  {
    vector<int> a2, b2;
    for (int i = 0, _n = (int)(N); i < _n; ++i)
      if (a[i] != b[i]) {
        a2.push_back(a[i]);
        b2.push_back(b[i]);
      }
    a = a2;
    b = b2;
    N = a2.size();
  }
  vector<int> v = a;
  sort(v.begin(), v.end());
  int U = (int)(unique(v.begin(), v.end()) - v.begin());
  if (xa == xb && iso) {
    a = normalize(a);
    b = normalize(b);
    UnionFind uf(U);
    int ans = 0;
    for (int i = 0, _n = (int)(N); i < _n; ++i) {
      if (a[i] != b[i]) {
        ans++;
        uf.link(a[i], b[i]);
      }
    }
    ans += uf.num();
    cout << ans << endl;
  } else {
    a = normalize(a);
    b = normalize(b);
    UnionFind uf(U);
    int ans = 0;
    for (int i = 0, _n = (int)(N); i < _n; ++i) {
      if (a[i] != b[i]) {
        ans++;
        uf.link(a[i], b[i]);
      }
    }
    ans += uf.num();
    ans--;
    cout << ans << endl;
  }
  return 0;
}
int main() {
  for (; !cin.eof(); cin >> ws) main2();
  return 0;
}
