#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=500005,inf=0x3f3f3f3f;
int read(){
	int f=1,g=0;
	char ch=getchar();
	for (;!isdigit(ch);ch=getchar()) if (ch=='-') f=-1;
	for (;isdigit(ch);ch=getchar()) g=g*10+ch-'0';
	return f*g;
}
int n,m,L,cnt,h[N],b[N],sl[N],sr[N];
ll ans;
typedef pair<int,int> pr;
priority_queue<pr,vector<pr>,greater<pr> > f;
struct node{int val,l,r,pre,nex;}a[N];
int getsum(int *s,int l,int r){return s[r]-s[l-1];}
void split(int x){
	int cur=x;m=0;
	while (a[x].val==a[cur].val){
		b[++m]=cur;
		sl[m]=sl[m-1]+a[cur].l;
		sr[m]=sr[m-1]+a[cur].r;
		cur=a[cur].nex;
	}
}
int getpre(int x){while(a[a[x].pre].val==a[x].val) x=a[x].pre;return x;}
void recalc(int x,int y){
	split(getpre(x));
	for (int i=1;i<=m;i++) if (b[i]==x) {x=i;break;}
	for (int i=1;i<=m;i++) if (b[i]==y) {y=i;break;}
	for (int i=1;i<=y;i++){
		int l=max((i<x) ? x : (y+1),i+L-1),r=m;
		if (l<=r) ans+=(ll)a[b[i]].l*getsum(sr,l,r);
	}
}
void printch(int x){
	while (a[x].pre) x=a[x].pre;
	for (;x;x=a[x].nex) printf("%d ",x);
	printf("\n");
}
int main(){
	n=read();L=read();ans=cnt=n;
	for (int i=1;i<=n;i++){
		a[i].val=read();a[i].l=a[i].r=1;
		a[i].pre=i-1;a[i].nex=i+1;h[i]=1;
		f.push(make_pair(a[i].val,i));
	}
	a[n].nex=0;
	for (int i=1,j;i<=n;i=j){
		for (j=i;a[i].val==a[j].val;j++);
		int len=j-i;
		if (len>=L) ans+=(ll)(len-L+1)*(len-L+2)/2;
	}
	while (1){
		while (!h[f.top().second]) f.pop();
		int x=getpre(f.top().second),y;f.pop();
		//printch(x);
		if (a[x].val>=inf) break;
		split(x);
		for (int i=1;i<=m;i++) h[b[i]]=0;
		x=a[b[1]].pre,y=a[b[m]].nex;
		if (m<L){
			a[++cnt].val=inf;a[cnt].pre=x;a[cnt].nex=y;
			if (x) a[x].nex=cnt;
			if (y) a[y].pre=cnt;
			h[cnt]=1;f.push(make_pair(a[cnt].val,cnt));
			continue;
		}
		int r=m/L;
		for (int i=1;i<=r;i++){
			a[cnt+i].val=a[b[1]].val+1;
			int tl=m-(r-i+1)*L+1,tr=max(1,tl-(L-1));
			a[cnt+i].l=getsum(sl,tr,tl);
			tl=i*L,tr=min(m,tl+L-1);
			a[cnt+i].r=getsum(sr,tl,tr);
			a[cnt+i].pre=(i!=1) ? (cnt+i-1) : x;
			a[cnt+i].nex=(i!=r) ? (cnt+i+1) : y;
			h[cnt+i]=1;f.push(make_pair(a[cnt+i].val,cnt+i));
		}
		if (x) a[x].nex=cnt+1;
		if (y) a[y].pre=cnt+r;
		recalc(cnt+1,cnt+r);
		cnt+=r;
	}
	printf("%lld\n",ans);
	return 0;
}