#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int a[N];
int b[N];
pair<int, int> p[N], q[N];
map<int, int> z;
int n;
int f[N];
int r[N], cnt;
int sz[N];
int main() {
  scanf("%d", &n);
  int x = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    x ^= a[i];
    z[a[i]]++;
    p[i].first = a[i];
    p[i].second = i;
  }
  z[x]++;
  p[n + 1].first = x;
  p[n + 1].second = n + 1;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    if (z[b[i]] == 0) {
      puts("-1");
      return 0;
    }
    z[b[i]]--;
    q[i].first = b[i];
    q[i].second = i;
  }
  x = n + 1;
  bool flag = true;
  for (int i = 1; i <= n; i++) {
    if (a[i] != b[i]) {
      flag = false;
    }
    if (z[a[i]] > 0) {
      z[a[i]]--;
      x = i;
      swap(p[n + 1], p[i]);
    }
  }
  if (flag) {
    puts("0");
    return 0;
  }
  sort(p + 1, p + n + 1);
  sort(q + 1, q + n + 1);
  f[p[n + 1].second] = n + 1;
  for (int i = 1; i <= n; i++) {
    f[p[i].second] = q[i].second;
  }
  for (int i = 1; i <= n; i++)
    if (!r[i]) {
      cnt++;
      for (int j = i; !r[j]; j = f[j]) {
        r[j] = cnt;
        sz[cnt]++;
      }
    }
  int ans = 0;
  for (int i = 1; i <= cnt; i++) {
    if (sz[i] == 1)
      ;
    else if (r[n + 1] == i)
      ans += sz[i] - 1;
    else
      ans += sz[i] + 1;
  }
  printf("%d\n", ans);
}
