#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 10;
const int M = (int)2018 * 2018;
const int K = (int)15;
const int INF = (int)1e9 + 7;
const int mod = (int)998244353;
const long double EPS = (long double)1e-9;
const long long LINF = (long long)1e18;
int n;
int a[N];
int b[N];
multiset<int> A, B;
int main() {
  srand(time(0));
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[n + 1] ^= a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    b[n + 1] ^= b[i];
  }
  for (int i = 1; i <= n + 1; i++) {
    A.insert(a[i]);
    B.insert(b[i]);
  }
  if (A != B) {
    cout << -1;
    return 0;
  }
  int ans = 0;
  while (1) {
    int id = -1;
    for (int i = 1; i <= n + 1; i++) {
      if (a[i] != b[i]) {
        if (a[n + 1] == b[i]) {
          id = i;
        }
      }
    }
    if (id == -1) break;
    ans++;
    swap(a[id], a[n + 1]);
  }
  int c = 0;
  for (int i = 1; i <= n + 1; i++) {
    if (a[i] != b[i]) {
      c++;
    }
  }
  cout << ans + 2 * c - 1;
  return 0;
}
