#include <bits/stdc++.h>
using namespace std;
map<int, int> mp, mp1, mp2, sum;
set<int> st[100010], h;
int a[100010], b[100010], n, cnt;
inline int rd() {
  int x = 0;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    ;
  for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
  return x;
}
int main() {
  n = rd();
  for (int i = 1; i <= n; i++) a[i] = rd();
  for (int i = 1; i <= n; i++) b[i] = rd();
  int s = 0;
  for (int i = 1; i <= n; i++) s ^= a[i];
  for (int i = 1; i <= n; i++) mp1[a[i]]++;
  mp1[s]++;
  for (int i = 1; i <= n; i++) mp2[b[i]]++;
  for (int i = 1; i <= n; i++)
    if (mp2[b[i]] > mp1[b[i]]) {
      puts("-1");
      return 0;
    }
  cnt = 0;
  for (int i = 1; i <= n; i++)
    if (!mp[a[i]]) mp[a[i]] = ++cnt;
  if (!mp[s]) mp[s] = ++cnt;
  int ans = 0, tt = 0;
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i])
      st[mp[b[i]]].insert(i), h.insert(i), sum[b[i]]++;
    else
      tt++;
  while (tt < n) {
    if (st[mp[s]].empty()) {
      ans++;
      set<int>::iterator it = h.begin();
      swap(s, a[*it]);
      continue;
    }
    set<int>::iterator hh = st[mp[s]].end();
    for (set<int>::iterator it = st[mp[s]].begin(); it != st[mp[s]].end(); it++)
      if (sum[a[*it]]) {
        hh = it;
        break;
      }
    if (hh != st[mp[s]].end()) {
      int x = *hh;
      swap(a[x], s);
      ans++;
      tt++;
      st[mp[s]].erase(x);
      sum[b[x]]--;
      h.erase(x);
    } else {
      set<int>::iterator it = h.begin();
      while (b[*it] == s) it++;
      int x = *it;
      swap(a[x], s);
      ans++;
    }
    if (tt == n) break;
  }
  printf("%d\n", ans);
  return 0;
}
