#include<bits/stdc++.h>
using namespace std;
namespace io{
	const int l=1<<20;
	char buf[l],*s,*t,c;
	inline void gc(){
		if(s==t){
			t=(s=buf)+fread(buf,1,l,stdin);
			c=s==t?EOF:*s++;
		}else c=*s++;
	}
	template<class IT>inline void gi(IT &x){
		x=0;gc();while(c<'0'||c>'9')gc();
		while('0'<=c&&c<='9'){x=(x<<1)+(x<<3)+(c^48);gc();}
	}
	char buf0[20];int a;
	template<class IT>inline void pi(IT x){
		if(x<0){putchar('-');x=-x;}
		do buf0[++a]=x%10+48;while(x/=10);
		while(a)putchar(buf0[a--]);
		putchar('\n');
	}
};
using io::gi;
using io::pi;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<int,ll > pil;
typedef pair<ll ,int> pli;
typedef pair<ll ,ll > pll;
#define pque priority_queue
#define rep(i,l,r) for(i=(l);i<=(r);++i)
#define per(i,l,r) for(i=(l);i>=(r);--i)
#define REP(i,l,r) for(i=(l);i< (r);++i)
#define PER(i,l,r) for(i=(l);i> (r);--i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define fi first
#define se second
template<class IT>inline void cmax(IT &a,IT b){if(a<b)a=b;}
template<class IT>inline void cmin(IT &a,IT b){if(b<a)a=b;}
const int N=200005;
struct node{
	int a,L,R,l,r;
	inline bool operator<(const node &x)const{
		return a<x.a||(a==x.a&&L<x.L);
	}
}z[N],x[N],y[N];ll ans,ant;
inline void calcs(int i,int j,int r){
	ll t=0ll;
	for(;j<=r;++i,++j)ans+=(t+=x[i].l)*x[j].r;
//	printf("i=%d j=%d r=%d ans=%lld\n",i,j,r,ans);
}
inline void calct(int i,int j,int r){
	ll t=0ll;
	for(;j<=r;++i,++j)ant+=(t+=z[i].l)*z[j].r;
//	printf("i=%d j=%d r=%d ant=%lld\n",i,j,r,ant);
}
inline void dbg(int X){
	int i;
	rep(i,1,X)printf("a=%d L=%d R=%d l=%d r=%d\n",x[i].a,x[i].L,x[i].R,x[i].l,x[i].r);puts("X");
}
int main(){
//	freopen("a.in","r",stdin);
//	freopen("a.out","w",stdout);
	int n,m,mn,X=0,Y=1,Z,T,i,j,k,l,c;
	scanf("%d%d",&n,&m);ans=n;
	rep(i,1,n){
		scanf("%d",&y[i].a);
		y[i].L=y[i].R=i;
		y[i].l=y[i].r=1;
	}
	sort(y+1,y+n+1);
	while(X||Y<=n){
		if(Y<=n){
			mn=y[Y].a;
			if(X)cmin(mn,x[X].a);
			while(y[Y].a==mn)x[++X]=y[Y++];
		}else mn=x[X].a;
		sort(x+1,x+X+1);
//		dbg(X);
		x[X+1].L=0;
		Z=0;
//		printf("===X=%d\n",X);
		for(i=j=1;i<=X;i=++j){
			for(;x[j+1].L==x[j].R+1;++j);
			c=(l=j+1-i)/m;
//			printf("i=%d j=%d\n",i,j);
			if(c){
				T=Z;
				REP(k,0,c){
					++T;
					z[T].a=mn+1;
					z[T].L=x[i+k*m    ].L;
					z[T].R=x[i+k*m+m-1].R;
					z[T].l=z[T].r=0;
				}
				z[T++].R=x[j].R;
				calcs(i  ,i+m-1,  j);
				rep(k,i+m-1,j)z[Z+(k-i+1)/m].r+=x[k].r;
				per(k,j-m+1,i)z[T-(j-k+1)/m].l+=x[k].l;
				calct(Z+1,Z+m  ,--T);
				Z=T;
			}
		}
		X=Z;
		rep(i,1,X)x[i]=z[i];
//		dbg(X);
	}
	printf("%lld",ans-ant);
	return 0;
}