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();
    }

    int gap;
    int beerCount;
    int[] locations;

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

        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;
        int locMin = locations[0];
        int locMax = locations[beerCount-1];

        int priceMin = resolvePrice(locMin);
        int priceMax = resolvePrice(locMax);

        /*while(true) {
            int locCurr = (locMin + locMax) / 2;
            int currPrice = resolvePrice(locCurr);
            if (currPrice < min) {
                min = currPrice;
            }
            if (locMin == locMax) {
                break;
            }
            if (locMin == locMax-1) {
                min = Math.min(priceMin, priceMax);
                break;
            }
            if (priceMin > priceMax) {
                locMin = locCurr;
                priceMin = currPrice;
            } else {
                locMax = locCurr;
                priceMax = currPrice;
            }

        }
        System.out.println(min);*/


        for (int x = locMin; x <= locMax; x++) {
            //int currPrice = resolvePrice(x);
            int currPrice = 0;
            for (int i = 0; i < beerCount; i++) {
                int currLoc = locations[i];
                int wantedLoc = x + i * gap;
                currPrice += Math.abs(currLoc - wantedLoc);
                if (currPrice > min) {
                    break;
                }
            }
            min = Math.min(currPrice, min);
        }
        System.out.println(min);

    }

    private int resolvePrice(int x) {
        int ret = 0;
        for (int i = 0; i < beerCount; i++) {
            int currLoc = locations[i];
            int wantedLoc = x + i * gap;
            ret += Math.abs(currLoc - wantedLoc);
        }
        return ret;
    }
}

