#include <bits/stdc++.h>
using namespace std;
int n, a[100005], b[100005], x, ans;
bool vis[100005];
map<int, int> mp, pos;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    x ^= a[i];
    mp[a[i]]++;
  }
  mp[x]++;
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  for (int i = 1; i <= n; i++) {
    if (!mp[b[i]]) {
      printf("-1\n");
      return 0;
    }
    mp[b[i]]--, pos[b[i]] = i;
  }
  bool f = true;
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) {
      f = false;
      break;
    }
  if (f) {
    printf("0\n");
    return 0;
  }
  for (int i = 1; i <= n; i++)
    if (!vis[i]) {
      int g = 0, cnt = 0;
      for (int j = i; !vis[j];
           vis[j] = true, j = pos[a[j]] ? pos[a[j]] : pos[x]) {
        cnt++;
        if (x == a[j])
          g = 1;
        else if (!pos[a[j]])
          g = 2;
      }
      if (!g)
        ans += 3 * (cnt - 1);
      else if (g == 1)
        ans += 3 * (cnt - 1) - 1;
      else {
        if (cnt == 1)
          ans++;
        else
          ans += 3 * (cnt - 2) + 2;
      }
    }
  printf("%d\n", ans);
}
