#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
int A[N], B[N];
int a[N], b[N];
int QuChong(int n) {
  int cnt = 0;
  for (int i = 1; i <= n; i += 1)
    if (A[i] != B[i]) a[++cnt] = A[i], b[cnt] = B[i];
  return cnt;
}
map<int, int> S1, S2;
int vis[N], siz[N];
int main() {
  int n;
  int yihuohe = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i += 1) scanf("%d", &A[i]);
  for (int j = 1; j <= n; j += 1) scanf("%d", &B[j]);
  for (int i = 1; i <= n; i += 1) yihuohe ^= A[i];
  int cnt = QuChong(n);
  for (int i = 1; i <= cnt; i += 1) S1[a[i]] = i, S2[b[i]] = i;
  int numdiff = 0;
  for (int i = 1; i <= cnt; i += 1)
    if (not S2.count(a[i])) numdiff += 1;
  if (numdiff > 1) {
    printf("-1\n");
    return 0;
  }
  int res = cnt;
  int temp = yihuohe, tmp;
  for (int i = 1; i <= cnt; i += 1) {
    if (a[i] == b[i]) continue;
    while (S2[temp] and not vis[S2[temp]]) {
      tmp = temp, temp = a[S2[temp]];
      a[S2[tmp]] = tmp, vis[S2[tmp]] = true;
    }
    if (a[i] == b[i]) continue;
    tmp = temp, res += 1, temp = a[i], a[i] = tmp;
  }
  printf("%d\n", res);
  return 0;
}
