#include<bits/stdc++.h>
#define llong long long
#define mkpr make_pair
#define pii pair<int,int>
#define riterator reverse_iterator
using namespace std;

inline int read()
{
	int x = 0,f = 1; char ch = getchar();
	for(;!isdigit(ch);ch=getchar()) {if(ch=='-') f = -1;}
	for(; isdigit(ch);ch=getchar()) {x = x*10+ch-48;}
	return x*f;
}

const int N = 2e5;
priority_queue<pii,vector<pii>,greater<pii> > que;
int nxt[N+3],prv[N+3];
int a[N+3];
llong cl[N+3],cr[N+3],cl2[N+3],cr2[N+3];
vector<int> vec;
int n,m; llong ans;

void solve(int l,int r)
{
//	printf("solve %d %d\n",l,r);
	llong sum = 0ll;
	for(int i=m-1; i<vec.size(); i++)
	{
		sum += cl[vec[i-m+1]];
		ans += cr[vec[i]]*sum;
	}
	int cnt = vec.size()/m;
	for(int i=l; i<=l+cnt-1; i++) cl2[i] = cr2[i] = 0ll;
	for(int i=vec.size()-m,j=0,k=l+cnt-1; i>=0; i--)
	{
		cl2[k] += cl[vec[i]];
		if(j==m-1) {k--,j=0;}
		else {j++;}
	}
	for(int i=m-1,j=0,k=l; i<vec.size(); i++)
	{
		cr2[k] += cr[vec[i]];
		if(j==m-1) {k++,j=0;}
		else {j++;}
	}
	for(int i=l; i<=l+cnt-1; i++) cl[i] = cl2[i],cr[i] = cr2[i];
	sum = 0ll;
	for(int i=l+m-1; i<=l+cnt-1; i++)
	{
		sum += cl[i-m+1];
		ans -= cr[i]*sum;
	}
	if(cnt==0)
	{
		nxt[prv[l]] = n+1; prv[nxt[r]] = 0;
	}
	else
	{
		nxt[l+cnt-1] = nxt[r]; prv[nxt[r]] = l+cnt-1;
		for(int i=l; i<l+cnt-1; i++) nxt[i] = i+1;
		for(int i=l+1; i<=l+cnt-1; i++) prv[i] = i-1;
		int val = a[l]; for(int i=l; i<=l+cnt-1; i++) a[i] = val+1;
		for(int i=l; i<=l+cnt-1; i++) que.push(mkpr(a[i],i));
	}
//	printf("cnt=%d\n",cnt);
//	printf("prv: "); for(int i=1; i<=n+1; i++) printf("[%d]%d ",i,prv[i]); puts("");
//	printf("nxt: "); for(int i=0; i<=n; i++) printf("[%d]%d ",i,nxt[i]); puts("");
//	printf("cl: "); for(int i=1; i<=n; i++) printf("[%d]%lld ",i,cl[i]); puts("");
//	printf("cr: "); for(int i=1; i<=n; i++) printf("[%d]%lld ",i,cr[i]); puts("");
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1; i<=n; i++) scanf("%d",&a[i]),que.push(mkpr(a[i],i)),cl[i] = cr[i] = 1ll,nxt[i] = i+1,prv[i] = i-1;
	nxt[0] = 1,prv[n+1] = n;
	while(1)
	{
		int l = que.top().second,r = l; que.pop(); vec.clear(); vec.push_back(l);
		while(!que.empty() && que.top().first==a[l] && que.top().second==nxt[r]) {r = nxt[r]; vec.push_back(r); que.pop();}
		solve(l,r);
		if(que.empty()) break;
	}
	printf("%lld\n",ans+n);
	return 0;
}
/*
4 3
4 3 4 4
*/