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

        long min = Integer.MAX_VALUE;
        long locMin = locations[0]-(beerCount*gap)/2;
        long locMax = locations[beerCount-1] - (beerCount*gap)/2;

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

        /*while(true) {
            long locCurr = (locMin + locMax) / 2;
            long 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);*/

        long sum = 0;
        for (int i = 0; i < beerCount; i++) {
            sum += locations[i];
        }
        long average = sum / beerCount;
        int median = locations[beerCount/2];
        int length = beerCount * gap;
        long start, end;
        if (median > average) {
            start = average;
            end = median;
        } else {
            end = average;
            start = median;
        }

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

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

    }

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

