#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int a[N], n, b[N], tmp, ans, cur;
map<int, int> mp;
multiset<pair<int, int> > st;
multiset<pair<int, int> >::iterator t;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), tmp ^= a[i];
  for (int i = 1; i <= n; ++i) scanf("%d", &b[i]);
  for (int i = 1; i <= n; ++i) ++mp[a[i]];
  ++mp[tmp];
  for (int i = 1; i <= n; ++i) {
    if (!mp.count(b[i])) return puts("-1"), 0;
    if (mp[b[i]] == 0) return puts("-1"), 0;
    mp[b[i]]--;
  }
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i]) st.insert(pair<int, int>(b[i], a[i]));
  cur = tmp;
  while (!st.empty()) {
    t = st.lower_bound(pair<int, int>(cur, 0));
    if (t == st.end() || t->first != cur) {
      t = st.begin();
      ++ans;
    }
    st.erase(t);
    cur = t->second;
    ++ans;
  }
  printf("%d\n", ans);
  return 0;
}
