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

typedef long long LL;

const int N=200000;

int n,m,a[N+9];

void into(){
  scanf("%d%d",&n,&m);
  for (int i=1;i<=n;++i)
	scanf("%d",&a[i]);
}

int sta[N+9],cst;
int pre[N+9],suf[N+9],spre[N+9],ssuf[N+9];
LL ans;

void Get_ans0(){
  int now=sta[cst],cnt=0;
  for (;cst&&sta[cst]==now;--cst) ++cnt;
  if (cnt<m){
	for (;cst;) Get_ans0();
	return;
  }
  int c=cnt/m;
  for (int i=1;i<=cnt;++i) spre[i]=spre[i-1]+pre[i+cst],ssuf[i]=ssuf[i-1]+suf[i+cst];
  for (int i=m;i<=cnt;++i) ans+=(LL)spre[i-m+1]*suf[i+cst];
  for (int i=1;i<=c;++i){
	pre[i+cst]=spre[cnt-m*(c-i+1)+1]-spre[max(cnt-m*(c-i+2)+1,0)];
	suf[i+cst]=ssuf[min(m*(i+1)-1,cnt)]-ssuf[m*i-1];
  }
  for (int i=m,s=0;i<=c;++i) ans-=(LL)(s+=pre[cst+i-m+1])*suf[i+cst];
  for (int i=1;i<=c;++i) sta[++cst]=now+1;
}

void Get_ans(){
  for (int i=1;i<=n;++i){
	for (;cst&&sta[cst]<a[i];) Get_ans0();
	sta[++cst]=a[i];
	pre[cst]=suf[cst]=1;
  }
  for (;cst;) Get_ans0();
}

void work(){
  Get_ans();
}

void outo(){
  printf("%lld\n",ans+n);
}

int main(){
  into();
  work();
  outo();
  return 0;
}