import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Marathon {

    public static void main(String[] args) throws IOException {
        new Marathon().solve();
    }

    private void solve() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] line = br.readLine().split(" ");
        int beerCount = Integer.parseInt(line[0]);
        int gap = Integer.parseInt(line[1]);

        int[] locations = new int[beerCount];
        line = br.readLine().split(" ");
        for (int i = 0; i < beerCount; i++) {
            locations[i] = Integer.parseInt(line[i]);
        }
        Arrays.sort(locations);

        int min = Integer.MAX_VALUE;
        for (int x = locations[0]; x < locations[beerCount-1]; x++) {
            int curr = 0;
            for (int i = 0; i < beerCount; i++) {
                int currLoc = locations[i];
                int wantedLoc = x + i * gap;
                curr += Math.abs(currLoc - wantedLoc);
            }
            min = Math.min(curr, min);
        }
        System.out.println(min);

    }
}

