#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define MAX 100002 int n; int k; long long int p[MAX]; long long int b[MAX]; bool visited[MAX]; long long int dist = 0; long long int summ = 0; bool flag[MAX]; inline void dfs(int a){ if (a == n){ k--; summ += dist; return; } int be = 1; if (!visited[a]){ be = p[a]; visited[a] = true; } for (int i = be; i <= n; i++){ if (flag[i]){ continue; } flag[i] = true; dist += (long long int)(abs(i - b[a])); dfs(a + 1); dist -= (long long int)(abs(i - b[a])); flag[i] = false; if (k == 0){ return; } } } int main(){ scanf("%d%d", &n, &k); for (int i = 0; i < n; i++){ scanf("%d", &p[i]); } for (int i = 0; i < n; i++){ scanf("%d", &b[i]); } int countt3 = 0; if (n < 10){ return 0; } while (k){ dfs(0); } printf("%lld\n", summ); return 0; }