#include <bits/stdc++.h>
void swap(long long *a, long long *b) {
  long long c;
  c = *b;
  *b = *a;
  *a = c;
}
long long max2(long long a, long long b) { return a >= b ? a : b; }
long long min2(long long a, long long b) { return a >= b ? b : a; }
long long minn(long long n, long long a[n]) {
  long long b = (1LL << 60);
  for (long long i = (0); i < (n); i++) b = min2(b, a[i]);
  return b;
}
long long maxn(long long n, long long a[n]) {
  long long b = -(1LL << 60);
  for (long long i = (0); i < (n); i++) b = max2(b, a[i]);
  return b;
}
long long ABS(long long a) { return a >= 0 ? a : (-a); }
long long POW(long long a, long long b) {
  long long c = 1;
  for (long long i = (0); i < (b); i++) c *= a;
  return c;
}
int upll(const void *a, const void *b) {
  return *(long long *)a < *(long long *)b   ? -1
         : *(long long *)a > *(long long *)b ? 1
                                             : 0;
}
int downll(const void *a, const void *b) {
  return *(long long *)a < *(long long *)b   ? 1
         : *(long long *)a > *(long long *)b ? -1
                                             : 0;
}
void sortup(long long *a, int n) { qsort(a, n, sizeof(long long), upll); }
void sortdown(long long *a, int n) { qsort(a, n, sizeof(long long), downll); }
typedef struct {
  long long aa;
  long long bb;
} frequent;
long long factorial(long long a) { return a == 1 ? a : a * factorial(a - 1); }
long long permutation(long long n, long long a[n]) {
  long long ans = 0;
  for (long long i = (0); i < (n - 1); i++) {
    long long c = 0;
    for (long long j = (i + 1); j < (n); j++)
      if (a[i] > a[j]) c++;
    ans += c * factorial(n - i - 1);
  }
  return ans;
}
int main(void) {
  long long n;
  scanf("%lld", &n);
  long long p[n], q[n];
  for (long long i = (0); i < (n); i++) {
    scanf("%lld", &p[i]);
  }
  for (long long i = (0); i < (n); i++) {
    scanf("%lld", &q[i]);
  }
  long long P = permutation(n, p), Q = permutation(n, q);
  printf("%lld\n", ABS(P - Q));
  return 0;
}
