#include <bits/stdc++.h>
using namespace std;
void read(int& x) { scanf("%d", &x); }
void read(long long& x) { scanf("%lld", &x); }
template <typename T, typename... Args>
void read(T& a, Args&... b) {
  read(a);
  read(b...);
}
int n;
int a[100010];
int b[100010];
const int inf = 1e9;
bool val() {
  int first = 0, second = 0;
  vector<int> vx, vy;
  for (int i = 1; i <= n; ++i) {
    first ^= a[i];
    second ^= b[i];
    vx.push_back(a[i]);
    vy.push_back(b[i]);
  }
  a[n + 1] = first;
  b[n + 1] = second;
  vx.push_back(first);
  vy.push_back(second);
  sort((vx).begin(), (vx).end());
  sort((vy).begin(), (vy).end());
  for (int i = 0; i <= n; ++i)
    if (vx[i] != vy[i]) return 0;
  return 1;
}
set<int> need[100010];
set<int> huan;
int um;
int ans;
void Swap(int p, int q) {
  ++ans;
  if (b[p] != a[p]) need[b[p]].erase(p), huan.erase(p), --um;
  if (b[q] != a[q]) need[b[q]].erase(q), huan.erase(q), --um;
  swap(a[p], a[q]);
  if (b[p] != a[p]) need[b[p]].insert(p), huan.insert(p), ++um;
  if (b[q] != a[q]) need[b[q]].insert(q), huan.insert(q), ++um;
}
int main() {
  read(n);
  for (int i = 1; i <= n; ++i) read(a[i]);
  for (int i = 1; i <= n; ++i) read(b[i]);
  if (!val()) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i <= n + 1; ++i)
    if (b[i] != a[i]) need[b[i]].insert(i), huan.insert(i), ++um;
  while (um) {
    if (b[n + 1] == a[n + 1] && need[a[n + 1]].empty()) {
      int first = *huan.begin();
      Swap(first, n + 1);
    }
    int second = *need[a[n + 1]].begin();
    Swap(n + 1, second);
  }
  printf("%d\n", ans);
  return 0;
}
