import std.algorithm; import std.array; import std.ascii; import std.container; import std.conv; import std.math; import std.numeric; import std.range; import std.stdio; import std.string; import std.typecons; void log(A...)(A arg) { stderr.writeln(arg); } int size(T)(in T s) { return cast(int)s.length; } long calc(in long[] a, in long[] b) { assert(a.size == b.size); long ans = 0; foreach (i; 0 .. a.size) { ans += abs(a[i] - b[i]); } return ans; } void main() { int N, K; readf("%d %d\n", &N, &K); auto P = readln.chomp.split(" ").map!(to!long).array; auto B = readln.chomp.split(" ").map!(to!long).array; if (K == 0) { writeln(0); return; } if (N <= 10) { long ans = 0; do { ans += calc(P, B); K--; nextPermutation(P); } while (K > 0); writeln(ans); return; } long[] yabaikeesu; for (long i = N - 9; i <= N; i++) { yabaikeesu ~= i; } long ans = 0; long base = calc(P[0 .. $ - 10], B[0 .. $ - 10]); auto p = P[$ - 10 .. $]; auto b = B[$ - 10 .. $]; do { ans += base + calc(p, b); nextPermutation(p); if (p == yabaikeesu) { auto wp = P[0 .. $ - 10] ~ p; nextPermutation(wp); base = calc(wp[0 .. $ - 10], B[0 .. $ - 10]); p = wp; } else { K--; } } while (K > 0); writeln(ans); }