#include <bits/stdc++.h>
using namespace std;

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf=1e9;
const int64_t inf64=1e18;
const double eps=1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
    os << "[";
    for (const auto &v : vec) {
    	os << v << ",";
    }
    os << "]";
    return os;
}

using i64=int64_t;

i64 gcd(i64 a,i64 b){
	if(b==0) return a;
	return gcd(b,a%b);
}
i64 lcm(i64 a,i64 b){
	return a/gcd(a,b)*b;
}

void solve(){
    int n,m;
    cin >>  n >> m;
    vector<int> a(n);
    rep(i,0,n) cin >> a[i];
    int ans=0;
    i64 l=lcm(n,m);
    vector<int> v;
    rep(i,0,l){
        v.push_back(a[i%n]);
        if(v.size()==m){
            int mi=inf,ma=0;
            rep(j,0,m){
                mi=min(mi,v[j]);
                ma=max(ma,v[j]);
            }
            ans+=ma-mi;
            v.clear();
        }
    }
    cout << ans << endl;
}

int main(){
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(10);
    solve();
    return 0;
}