#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B>
bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
const double PI = acos(-1);
const double EPS = 1e-9;
long long inf = sizeof(long long) == sizeof(long long) ? 1e18 + 10 : 1e9 + 10;
long long dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
long long dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
struct UnionFind {
  vector<long long> data;
  UnionFind(long long size) : data(size, -1) {}
  bool unionSet(long long x, long long y) {
    x = root(x);
    y = root(y);
    if (x == y) return 0;
    if (data[y] < data[x]) swap(x, y);
    data[x] += data[y], data[y] = x;
    return 1;
  }
  bool findSet(long long x, long long y) { return root(x) == root(y); }
  long long root(long long x) {
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  long long size(long long x) { return -data[root(x)]; }
};
signed main() {
  long long n;
  cin >> n;
  vector<long long> a(n), b(n);
  long long x = 0;
  for (long long i = 0; i < n; i++) {
    long long c;
    cin >> c;
    x ^= c;
    a[i] = c;
  }
  a.push_back(x);
  x = 0;
  for (long long i = 0; i < n; i++) {
    long long c;
    cin >> c;
    x ^= c;
    b[i] = c;
  }
  b.push_back(x);
  {
    vector<long long> c = a, d = b;
    sort(c.begin(), c.end());
    sort(d.begin(), d.end());
    bool h = true;
    for (long long i = 0; i < n + 1; i++)
      if (c[i] - d[i]) h = false;
    if (!h) {
      cout << -1 << endl;
      return 0;
    }
  }
  map<long long, long long> ma;
  long long t = 1;
  for (long long i = 0; i < a.size(); i++)
    if (!ma[a[i]]) ma[a[i]] = t++;
  for (long long i = 0; i < b.size(); i++)
    if (!ma[b[i]]) ma[b[i]] = t++;
  long long out = -1;
  UnionFind uf(t);
  for (long long i = 0; i < n; i++)
    if (a[i] - b[i]) {
      out++;
      uf.unionSet(ma[a[i]], ma[b[i]]);
    }
  for (long long i = 0; i < t; i++)
    if (uf.root(i) == i && uf.size(i) > 1) out++;
  if (out < 0) out = 0;
  cout << out << endl;
}
