#include<bits/stdc++.h>
#define For(i,x,y) for (register int i=(x);i<=(y);i++)
#define FOR(i,x,y) for (register int i=(x);i<(y);i++)
#define Dow(i,x,y) for (register int i=(x);i>=(y);i--)
#define Debug(v) for (auto i:v) printf("%lld ",i);puts("")
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define ep emplace_back
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define fil(a,b) memset((a),(b),sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pa;
typedef pair<ll,ll> PA;
typedef vector<int> poly;
inline ll read(){
    ll x=0,f=1;char c=getchar();
    while ((c<'0'||c>'9')&&(c!='-')) c=getchar();
    if (c=='-') f=-1,c=getchar();
    while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    return x*f;
}

const int N = 2e5+10;
int n,m,a[N];
ll L[N],R[N],newL[N],newR[N];

int pre[N],nxt[N];
inline void Del(int x){
	pre[nxt[x]]=pre[x],nxt[pre[x]]=nxt[x];
}

int main(){
	n=read(),m=read();
	For(i,1,n) a[i]=read();
	set<pa>s;
	For(i,1,n) s.insert(mp(a[i],i)),pre[i]=i-1,nxt[i]=i+1,L[i]=R[i]=1;
	ll ans=n;
	while (!s.empty()){
		//for (int i=1;i!=n+1;i=nxt[i]) printf("%d %d\n",i,a[i]);puts("");
		int u=(*s.begin()).se;s.erase(s.begin());
		int l=u,r=u;
		while (a[pre[l]]==a[l]) l=pre[l];
		while (a[nxt[r]]==a[r]) r=nxt[r];
		poly v;
		for (int i=l;i!=r;i=nxt[i]) v.pb(i); 
		v.pb(r);
		if (siz(v)<m){
			for (auto i:v) s.erase(mp(a[i],i)),Del(i);
			continue;
		}
		ll S=0;
		FOR(i,m-1,siz(v)) S+=L[v[i-m+1]],ans+=S*R[v[i]];
		l=m-1,r=l+m-1;
		int cnt=siz(v)/m;
		For(i,1,cnt){
			newR[i]=0;
			For(j,l,min(r,siz(v)-1)) newR[i]+=R[v[j]];
			l+=m,r+=m; 
		}
		r=siz(v)-m,l=r-m+1;
		Dow(i,cnt,1){
			newL[i]=0;
			For(j,max(l,0),r) newL[i]+=L[v[j]];
			l-=m,r-=m;
		}
		for (auto i:v) s.erase(mp(a[i],i));
		FOR(i,0,cnt){
			a[v[i]]++,L[v[i]]=newL[i+1],R[v[i]]=newR[i+1];
			s.insert(mp(a[v[i]],v[i]));
		}
		FOR(i,cnt,siz(v)) Del(v[i]);
		S=0;
		FOR(i,m-1,cnt) S+=L[v[i-m+1]],ans-=S*R[v[i]];
		s.insert(mp(a[v[0]],v[0]));
	}
	printf("%lld\n",ans);
}