#include <bits/stdc++.h>

using namespace std;

const bool debug = true;
#define dbg(...) if(debug) printf(__VA_ARGS__)
#define print(var) if (debug) cout << #var << " = " << var << endl

namespace {
    /** utility functions for reading a pair or a vector from stdin. */
    template<typename K, typename V> istream& operator>>(istream& is, pair<K, V>& p) { return is >> p.first >> p.second; }
    template<typename T> istream& operator>>(istream& is, vector<T>& xs) { for (auto& x : xs) is >> x; return is; }

    using ll = long long;
    int N, M;
    vector<int> A;
    void input() {
        cin >> N >> M;
        A.resize(N);
        cin >> A;
    }

    ll gcd(ll a, ll b) {
        if (a > b) swap(a, b);
        if (a == 0) return b;
        return gcd(b % a, a);
    }
    ll lcm(ll a, ll b) {
        ll g = gcd(a, b);
        return a / g * b;
    }    

    void solve() {
        vector<int> X;
        auto L = lcm(N, M);
        for (int i = 0; i < L; i += M) {
            X.insert(X.end(), A.begin(), A.end());
        }
        int i = 0;
        int ans = 0;
        while (true) {
            int j = i + M;
            int W = *max_element(X.begin() + i, X.begin() + j);
            int w = *min_element(X.begin() + i, X.begin() + j);
            ans += W - w;
            i = j;
            if (i == L) break;
        }
        cout << ans << endl;
    }
}

int main() {
    input(); solve();
    return 0;
}


