#include <bits/stdc++.h>
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
int N;
int A[101010], B[101010];
int rest = -1;
bool chk() {
  int h = 0;
  for (int i = 0; i < N; i++) h ^= A[i];
  map<int, int> cnt;
  for (int i = 0; i < N; i++) cnt[A[i]]++;
  cnt[h]++;
  for (int i = 0; i < N; i++) {
    if (cnt[B[i]] == 0) return false;
    cnt[B[i]]--;
  }
  for (auto p : cnt)
    if (p.second) rest = p.first;
  return true;
}
void _main() {
  cin >> N;
  for (int i = 0; i < N; i++) cin >> A[i];
  for (int i = 0; i < N; i++) cin >> B[i];
  if (!chk()) {
    printf("-1\n");
    return;
  }
  int h = 0;
  for (int i = 0; i < N; i++) h ^= A[i];
  int cnt = 0;
  int ans = 0;
  map<int, queue<int>> AA, BB;
  queue<int> ng;
  for (int i = 0; i < N; i++)
    if (A[i] != B[i]) {
      cnt++;
      AA[A[i]].push(i);
      BB[B[i]].push(i);
      ng.push(i);
    }
  int nh = rest;
  for (int i = 0; i < N; i++)
    if (B[i] == rest) nh = -1;
  while (cnt) {
    while (!BB[h].empty()) {
      int a = BB[h].front();
      if (A[a] == B[a])
        BB[h].pop();
      else
        break;
    }
    if (BB[h].size() == 0) {
      while (!ng.empty()) {
        int a = ng.front();
        if (A[a] == B[a])
          ng.pop();
        else {
          if (0 <= nh) {
            if (1 < ng.size() && A[a] == nh) {
              ng.pop();
              ng.push(a);
              continue;
            }
          }
          break;
        }
      }
      int a = ng.front();
      swap(h, A[a]);
      ans++;
    } else {
      if (0 <= nh) {
        if (1 < BB[h].size()) {
          if (A[BB[h].front()] == nh) {
            int a = BB[h].front();
            BB[h].pop();
            BB[h].push(a);
            break;
          }
        }
      }
      int a = BB[h].front();
      BB[h].pop();
      swap(h, A[a]);
      cnt--;
      ans++;
    }
  }
  printf("%d\n", ans);
}
