#include <bits/stdc++.h>
using namespace std;
int arr[100001], tree[400004];
void build(int node, int st, int en) {
  if (st > en) return;
  if (st == en) {
    tree[node] = arr[st];
    return;
  }
  int mid = (st + en) / 2;
  build(2 * node, st, mid);
  build(2 * node + 1, mid + 1, en);
  tree[node] = tree[2 * node] ^ tree[2 * node + 1];
}
void update(int node, int st, int en, int shemal, int yemeen, int val) {
  if (st > en || st > yemeen || en < shemal) return;
  if (shemal <= st && en <= yemeen) {
    tree[node] = val;
    return;
  }
  int mid = (st + en) / 2;
  update(2 * node, st, mid, shemal, yemeen, val);
  update(2 * node + 1, mid + 1, en, shemal, yemeen, val);
  tree[node] = tree[2 * node] ^ tree[2 * node + 1];
}
bool visited[100100];
int main() {
  int n;
  cin >> n;
  map<int, deque<int> > mp;
  set<pair<int, int> > st;
  for (auto i = 0; i < (long long)(n); i++) cin >> arr[i];
  vector<int> temp(n);
  int idx = 0, match = 0, cnt = 0;
  for (auto i = 0; i < (long long)(n); i++) {
    cin >> temp[i];
    if (temp[i] == arr[i])
      match++, visited[i] = true;
    else
      mp[temp[i]].push_back(i);
  }
  build(1, 0, n - 1);
  while (true) {
    if (idx == n && match != n) {
      cout << -1;
      return 0;
    }
    if (match == n) {
      cout << cnt;
      return 0;
    }
    int rakam = tree[1];
    if (mp[rakam].empty()) {
      while (visited[idx]) idx++;
      arr[idx] = rakam;
      update(1, 0, n - 1, idx, idx, rakam);
      visited[idx] = true;
    } else {
      int x = mp[rakam].front();
      mp[rakam].pop_front();
      arr[x] = rakam;
      visited[x] = true;
      match++;
      update(1, 0, n - 1, x, x, rakam);
    }
    cnt++;
  }
  return 0;
}
