#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll Mod = 1000000007;
int main() {
  int N;
  cin >> N;
  ll a[N];
  ll b[N];
  ll XOR = 0;
  map<ll, int> d1;
  map<ll, int> d2;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    d1[a[i]]++;
    XOR ^= a[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> b[i];
    d2[b[i]]++;
  }
  bool can = true;
  int can2 = 0;
  bool can3 = true;
  for (auto p : d2) {
    if (abs(d1[p.first] - p.second) >= 2) {
      can = false;
    } else if (abs(d1[p.first] - p.second) == 1) {
      if (p.first != XOR) {
        can3 = false;
      }
      can2++;
    }
  }
  if (!can || can2 >= 2 || !can3) {
    cout << -1 << endl;
    return 0;
  }
  map<ll, int> mp;
  vector<int> C[N + 1];
  int cnt = 1;
  int samecount = 0;
  for (int i = 0; i < N; i++) {
    if (a[i] == b[i]) {
      samecount++;
      continue;
    }
    if (mp[b[i]] == 0) {
      mp[b[i]] = cnt;
      cnt++;
    }
    C[mp[b[i]]].push_back(i);
  }
  int world[N];
  int tick[N + 1];
  tick[0] = 0;
  for (int i = 0; i < N; i++) {
    world[i] = 0;
    tick[i + 1] = 0;
  }
  int existXOR = -1;
  for (int i = 0; i < N; i++) {
    if (a[i] == b[i]) {
      continue;
    }
    if (b[i] == XOR) {
      existXOR = i;
      break;
    }
  }
  if (existXOR == -1) {
    int num = 1;
    for (int i = 0; i < N; i++) {
      if (a[i] == b[i]) {
        continue;
      }
      if (world[i] == 0) {
        int now = i;
        while (1 == 1) {
          world[now] = num;
          int f = mp[a[now]];
          now = C[mp[a[now]]][tick[mp[a[now]]]];
          tick[f]++;
          if (world[now] == num) {
            break;
          }
        }
        num++;
      }
    }
    cout << num - 1 + N - samecount << endl;
  } else {
    assert(1 != 1);
    int num = 1;
    int now = existXOR;
    while (1 == 1) {
      world[now] = num;
      int f = mp[a[now]];
      if (tick[mp[a[now]]] == C[mp[a[now]]].size()) {
        break;
      }
      now = C[mp[a[now]]][tick[mp[a[now]]]];
      tick[f]++;
      if (world[now] == num) {
        break;
      }
    }
    num++;
    for (int i = 0; i < N; i++) {
      if (a[i] == b[i]) {
        continue;
      }
      if (world[i] == 0) {
        now = i;
        while (1 == 1) {
          world[now] = num;
          int f = mp[a[now]];
          if (tick[mp[a[now]]] == C[mp[a[now]]].size()) {
            break;
          }
          now = C[mp[a[now]]][tick[mp[a[now]]]];
          tick[f]++;
          if (world[now] == num) {
            break;
          }
        }
        num++;
      }
    }
    cout << num - 1 - 1 + N - samecount << endl;
  }
  return 0;
}
