/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <bits/stdc++.h>
#define int long long
using namespace std;
bool isPrime(int n) {
if (n <= 1)
return false;
for (int i = 2; i*i <= n ; i++)
if (n % i == 0)
return false;
return true;
}
int nearPrime(int n) {
if (n<=1){
return 2;
}
int prime1 = n;
int prime2 = n;
bool found =false;
while (!found) {
prime1++;
if (isPrime(prime1))
found = true;
}
found =false;
while (!found) {
prime2--;
if (isPrime(prime2))
found = true;
}
if(prime1-n < n-prime2){
return prime1;
}
else return prime2;
}
int32_t main()
{
int n, k;
int a[10000];
int b[10000];
int c[10000];
int sum = 100000;
cin >> n >> k;
for (int i=0;i<n;i++) {
cin >> a[i];
}
for (int i=0;i<n;i++) {
if (isPrime(a[i])) {
b[i] = a[i];
} else {
b[i] = nearPrime(a[i]);
}
c[i] = abs(a[i] - b[i]);
}
/*for (int i=0;i<n;i++){
cout<< c[i];
}*/
for (int i=0;i<n-k;i++) {
int t = 0;
for(int j = i; j< i+k;j++){
t += c[j];
}
if(t < sum){
sum = t;
}
}
cout<< sum;
return 0;
}