#include <bits/stdc++.h>
#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
using namespace std;
using namespace tr1;
double __begin;
template <class T1, class T2, class T3>
struct triple {
  T1 a;
  T2 b;
  T3 c;
  triple() : a(T1()), b(T2()), c(T3()){};
  triple(T1 _a, T2 _b, T3 _c) : a(_a), b(_b), c(_c) {}
};
template <class T1, class T2, class T3>
bool operator<(const triple<T1, T2, T3>& t1, const triple<T1, T2, T3>& t2) {
  if (t1.a != t2.a)
    return t1.a < t2.a;
  else if (t1.b != t2.b)
    return t1.b < t2.b;
  else
    return t1.c < t2.c;
}
template <class T1, class T2, class T3>
bool operator>(const triple<T1, T2, T3>& t1, const triple<T1, T2, T3>& t2) {
  if (t1.a != t2.a)
    return t1.a > t2.a;
  else if (t1.b != t2.b)
    return t1.b > t2.b;
  else
    return t1.c > t2.c;
}
inline int bits_count(int v) {
  v = v - ((v >> 1) & 0x55555555);
  v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
  return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
}
inline int bits_count(long long v) {
  int t = v >> 32;
  int p = (v & ((1LL << 32) - 1));
  return bits_count(t) + bits_count(p);
}
unsigned int reverse_bits(register unsigned int x) {
  x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
  x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
  x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
  x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
  return ((x >> 16) | (x << 16));
}
template <class T>
inline int sign(T x) {
  return x > 0 ? 1 : x < 0 ? -1 : 0;
}
inline bool isPowerOfTwo(int x) { return (x != 0 && (x & (x - 1)) == 0); }
template <class T1, class T2, class T3>
T1 inline clamp(T1 x, const T2& a, const T3& b) {
  if (x < a)
    return a;
  else if (x > b)
    return b;
  else
    return x;
}
unsigned long long rdtsc() {
  unsigned long long ret;
  __asm__ __volatile__("rdtsc" : "=A"(ret) : :);
  return ret;
}
template <class T>
T read_int() {
  const int B = 4096;
  static char b[B + 16], *i = b, *e = b + B;
  if (*i == 0) {
    memset(b, 0, B);
    cin.read(b, B);
    i = b;
  }
  while (*i && (*i < '0' || *i > '9')) {
    ++i;
  }
  T r = 0;
  while (*i >= '0' && *i <= '9') {
    r = r * 10 + *i - '0';
    ++i;
    if (i == e) {
      memset(b, 0, B);
      cin.read(b, B);
      i = b;
    }
  }
  ++i;
  return r;
}
template <class T>
void write_int(T x, char endc = '\n') {
  const int B = 4096;
  static char outbuf[B + 20], *outp = outbuf, *loutp = outbuf + B;
  char* begp = outp;
  while (x) {
    T t = x / 10;
    char c = x - 10 * t + '0';
    *outp++ = c;
    x = t;
  }
  char* endp = outp - 1;
  while (begp < endp) {
    swap(*begp, *endp);
    ++begp;
    --endp;
  }
  if (outp > loutp) {
    cout.write(outbuf, outp - outbuf);
    outp = outbuf;
  }
  *outp++ = endc;
  struct F {
    F() {
      if (outp != outbuf) {
        cout.write(outbuf, outp - outbuf);
      }
    }
  };
  static F f;
}
template <class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p);
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v);
template <class T1, class T2>
ostream& operator<<(ostream& os, const set<T1, T2>& v);
template <class T1, class T2>
ostream& operator<<(ostream& os, const multiset<T1, T2>& v);
template <class T1, class T2>
ostream& operator<<(ostream& os, priority_queue<T1, T2> v);
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const map<T1, T2, T3>& v);
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const multimap<T1, T2, T3>& v);
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const triple<T1, T2, T3>& t);
template <class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  bool f = 1;
  os << "[";
  for (auto& i : v) {
    if (!f) os << ", ";
    os << i;
    f = 0;
  }
  return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, const set<T1, T2>& v) {
  bool f = 1;
  os << "[";
  for (auto& i : v) {
    if (!f) os << ", ";
    os << i;
    f = 0;
  }
  return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, const multiset<T1, T2>& v) {
  bool f = 1;
  os << "[";
  for (auto& i : v) {
    if (!f) os << ", ";
    os << i;
    f = 0;
  }
  return os << "]";
}
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const map<T1, T2, T3>& v) {
  bool f = 1;
  os << "[";
  for (auto& ii : v) {
    if (!f) os << ", ";
    os << "(" << ii.first << " -> " << ii.second << ") ";
    f = 0;
  }
  return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, const multimap<T1, T2>& v) {
  bool f = 1;
  os << "[";
  for (auto& ii : v) {
    if (!f) os << ", ";
    os << "(" << ii.first << " -> " << ii.second << ") ";
    f = 0;
  }
  return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, priority_queue<T1, T2> v) {
  bool f = 1;
  os << "[";
  while (!v.empty()) {
    auto x = v.top();
    v.pop();
    if (!f) os << ", ";
    f = 0;
    os << x;
  }
  return os << "]";
}
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const triple<T1, T2, T3>& t) {
  return os << "(" << t.a << ", " << t.b << ", " << t.c << ")";
}
template <class T1, class T2>
void printarray(T1 a, T2 sz, T2 beg = 0) {
  for (T2 i = beg; i < sz; i++) cout << a[i] << " ";
  cout << endl;
}
template <class T1, class T2, class T3>
inline istream& operator>>(istream& os, triple<T1, T2, T3>& t);
template <class T1, class T2>
inline istream& operator>>(istream& os, pair<T1, T2>& p) {
  return os >> p.first >> p.second;
}
template <class T>
inline istream& operator>>(istream& os, vector<T>& v) {
  if (v.size())
    for (T& t : v) os >> t;
  else {
    string s;
    T obj;
    while (s.empty()) {
      getline(os, s);
      if (!os) return os;
    }
    stringstream ss(s);
    while (ss >> obj) v.push_back(obj);
  }
  return os;
}
template <class T1, class T2, class T3>
inline istream& operator>>(istream& os, triple<T1, T2, T3>& t) {
  return os >> t.a >> t.b >> t.c;
}
template <class T1, class T2>
inline pair<T1, T2> operator+(const pair<T1, T2>& p1, const pair<T1, T2>& p2) {
  return pair<T1, T2>(p1.first + p2.first, p1.second + p2.second);
}
template <class T1, class T2>
inline pair<T1, T2> operator-(const pair<T1, T2>& p1, const pair<T1, T2>& p2) {
  return pair<T1, T2>(p1.first - p2.first, p1.second - p2.second);
}
template <class T>
bool isPrime(T x) {
  if (x < 2) return 0;
  for (T i = 2; i * i <= x; ++i)
    if (x % i == 0) return 0;
  return 1;
}
inline void normmod(long long& x, long long m) {
  x %= m;
  if (x < 0) x += m;
}
inline long long summodfast(long long x, long long y, long long m) {
  long long res = x + y;
  if (res >= m) res -= m;
  return res;
}
inline int summodfast(int x, int y, int m) {
  int res = x + y;
  if (res >= m) res -= m;
  return res;
}
inline void addmodfast(long long& x, long long y, long long m) {
  x += y;
  if (x >= m) x -= m;
}
inline void addmodfast(int& x, int y, int m) {
  x += y;
  if (x >= m) x -= m;
}
inline void submodfast(long long& x, long long y, long long m) {
  x -= y;
  if (x < 0) x += m;
}
inline void submodfast(int& x, int y, int m) {
  x -= y;
  if (x < 0) x += m;
}
inline long long mulmod(long long x, long long n, long long m) {
  long long res = 0;
  normmod(x, m);
  normmod(n, m);
  while (n) {
    if (n & 1) res = summodfast(res, x, m);
    x = summodfast(x, x, m);
    n >>= 1;
  }
  return res;
}
inline long long powmod(long long x, long long n, long long m) {
  long long res = 1;
  while (n) {
    if (n & 1) res = (res * x) % m;
    x = (x * x) % m;
    n >>= 1;
  }
  return res;
}
inline long long powmulmod(long long x, long long n, long long m) {
  long long res = 1;
  while (n) {
    if (n & 1) res = mulmod(res, x, m);
    x = mulmod(x, x, m);
    n >>= 1;
  }
  return res;
}
inline long long gcd(long long a, long long b) {
  long long t;
  while (b) {
    a = a % b;
    t = a;
    a = b;
    b = t;
  }
  return a;
}
inline int gcd(int a, int b) {
  int t;
  while (b) {
    a = a % b;
    t = a;
    a = b;
    b = t;
  }
  return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long gcd(long long a, long long b, long long c) {
  return gcd(gcd(a, b), c);
}
inline int gcd(int a, int b, int c) { return gcd(gcd(a, b), c); }
long long gcdex(long long a, long long b, long long& x, long long& y) {
  if (!a) {
    x = 0;
    y = 1;
    return b;
  }
  long long y1;
  long long d = gcdex(b % a, a, y, y1);
  x = y1 - (b / a) * y;
  return d;
}
int a[101010];
int b[101010];
int n;
map<int, vector<int>> v;
set<int> used;
void go(int cur) {
  used.insert(cur);
  for (int to : v[cur]) {
    if (!used.count(to)) {
      go(to);
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  int s = 0;
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    s ^= a[i];
  }
  a[n] = s;
  int an = a[n];
  s = 0;
  multiset<int> q;
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
    s ^= b[i];
    ans += a[i] != b[i];
    q.insert(b[i]);
  }
  if (ans == 0) {
    cout << 0 << endl;
    return 0;
  }
  b[n] = s;
  int bn = b[n];
  ++n;
  for (int i = 0; i < n; ++i) {
    if (a[i] != b[i]) {
      v[a[i]].push_back(b[i]);
    }
  }
  sort(a, a + n);
  sort(b, b + n);
  for (int i = 0; i < n; ++i) {
    if (a[i] != b[i]) {
      cout << "-1" << endl;
      return 0;
    }
  }
  --ans;
  for (int i = 0; i < n; ++i) {
    if (!used.count(a[i]) && !v[a[i]].empty()) {
      ++ans;
      go(a[i]);
    }
  }
  cout << ans << endl;
  return 0;
}
