#include <bits/stdc++.h>
double pi = 3.14159265358979323846;
using namespace std;
template <typename T>
std::ostream& operator<<(std::ostream& os, const vector<T>& z) {
  os << "[ ";
  for (int i = (0); i < (z.size()); ++i) os << z[i] << ", ";
  return (os << "]" << endl);
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const set<T>& z) {
  os << "set( ";
  for (auto p = (z).begin(); p != (z).end(); ++p) os << (*p) << ", ";
  return (os << ")" << endl);
}
template <typename T, typename U>
std::ostream& operator<<(std::ostream& os, const map<T, U>& z) {
  os << "{ ";
  for (auto p = (z).begin(); p != (z).end(); ++p)
    os << (p->first) << ": " << (p->second) << ", ";
  return (os << "}" << endl);
}
template <typename T, typename U>
std::ostream& operator<<(std::ostream& os, const pair<T, U>& z) {
  return (os << "(" << z.first << ", " << z.second << ",)");
}
double get_time() {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return tv.tv_sec + tv.tv_usec * 1e-6;
}
struct RND {
  unsigned int x;
  unsigned int y;
  unsigned int z;
  unsigned int w;
  RND() {
    x = 123456789;
    y = 362436069;
    z = 521288629;
    w = 88675123;
  }
  void init(int seed) {
    x = 123456789;
    y = 362436069;
    z = 521288629;
    w = seed + 100;
    for (int i = (0); i < (10); ++i) get();
  }
  unsigned int get() {
    unsigned int t;
    t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
    return w;
  }
};
RND rnd;
void _main(istream& inp) {
  long long n;
  inp >> n;
  vector<long long> a(n);
  vector<long long> b(n);
  for (int i = (0); i < (n); ++i) inp >> a[i];
  for (int i = (0); i < (n); ++i) inp >> b[i];
  long long x = 0;
  long long y = 0;
  for (int i = (0); i < (n); ++i) x ^= a[i];
  for (int i = (0); i < (n); ++i) y ^= b[i];
  map<long long, vector<long long> > desire;
  for (int i = (0); i < (n); ++i) {
    desire[a[i]] = vector<long long>();
    desire[b[i]] = vector<long long>();
    desire[x] = vector<long long>();
    desire[y] = vector<long long>();
  }
  for (int i = (0); i < (n); ++i)
    if (a[i] != b[i]) {
      desire[b[i]].push_back(i);
    }
  long long j = 0;
  long long cnt = 0;
  while (true) {
    if (desire[x].size() != 0) {
      int i = desire[x].back();
      desire[x].pop_back();
      swap(a[i], x);
      cnt++;
    } else {
      while (j < n && a[j] == b[j]) j++;
      if (j == n || cnt >= 1e6)
        break;
      else {
        swap(a[j], x);
        cnt++;
      }
    }
  }
  bool ok = true;
  for (int i = (0); i < (n); ++i)
    if (a[i] != b[i]) ok = false;
  if (x != y) ok = false;
  if (ok) {
    cout << cnt << endl;
  } else {
    cout << -1 << endl;
  }
}
int main() {
  if (0) {
    ifstream ifs("test.txt");
    _main(ifs);
  } else {
    _main(cin);
  }
  return 0;
}
