/*  -*- coding: utf-8 -*-
 *
 * 2806.cc: Weight Range
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 1000;

/* typedef */

/* global variables */

int as[MAX_N];

/* subroutines */

inline void setmin(int &a, int b) { if (a > b) a = b; }
inline void setmax(int &a, int b) { if (a < b) a = b; }

/* main */

int main() {
  int m, n;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d", as + i);

  int sum = 0;
  for (int i = 0, j = 0;; i++) {
    int mina = as[j], maxa = as[j];
    for (int k = 0; k < m; k++) {
      setmin(mina, as[j]);
      setmax(maxa, as[j]);
      j = (j + 1) % n;
    }
    sum += maxa - mina;
    if (j == 0) break;
  }

  printf("%d\n", sum);
  return 0;
}

