#include <bits/stdc++.h>
using namespace std;
const int N = 1E5 + 50;
int n, m, las, ans, tot, flag = 1, a[N], b[N], c[N];
vector<int> g, idx[N];
multiset<int> d;
int solve(bool flag = false, int start = 0) {
  g.push_back(m);
  sort(g.begin(), g.end());
  int los_num = (*d.begin()), loc;
  for (int i = 1; i <= n; i++) {
    ans += (a[i] != b[i]), flag |= (b[i] == m);
    if (a[i] == los_num) loc = i;
    if (flag && !start) start = i;
  }
  int l = lower_bound(c + 1, c + n, los_num) - c;
  if (!start) return n;
  for (int i = 1; i <= n; i++)
    a[i] = lower_bound(g.begin(), g.end(), a[i]) - g.begin() + 1,
    b[i] = lower_bound(g.begin(), g.end(), b[i]) - g.begin() + 1;
  for (int i = 1; i <= n; i++) idx[a[i]].push_back(i);
  int f = 0, now = loc;
  while (now != start) {
    int nex_col = b[now];
    f += b[now] != a[now];
    if (idx[nex_col].size() <= 0) break;
    now = idx[nex_col][idx[nex_col].size() - 1];
    idx[nex_col].pop_back();
  }
  return ans + !(f == ans - (g[a[start] - 1] != m));
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i), m ^= a[i], d.insert(a[i]), g.push_back(a[i]);
  for (int i = 1; i <= n; i++) {
    scanf("%d", b + i), flag &= (a[i] == b[i]);
    c[i] = b[i], g.push_back(b[i]);
  }
  if (flag) return 0 * printf("0\n");
  for (int i = 1; i <= n; i++)
    if (b[i] == m) {
      for (int j = i; j < n; j++) c[j] = c[j + 1];
      break;
    }
  sort(c + 1, c + n);
  bool jud = 1;
  for (int i = 1; i < n; i++) {
    multiset<int>::iterator it = d.find(c[i]);
    if (it == d.end())
      jud = 0;
    else
      d.erase(it);
  }
  if (!jud) return 0 * printf("-1\n");
  cout << solve() << '\n';
  return 0;
}
