#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int,int> pii;

const int maxn=200111;
int n,l,a[maxn],f[maxn],lv[maxn],rv[maxn];
LL ans;
int gf(int x){return x==f[x]?x:f[x]=gf(f[x]);}

int main()
{
	scanf("%d%d",&n,&l);
	
	set<pii> st;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",a+i);
		st.insert(mp(a[i],i));
		f[i]=i;lv[i]=rv[i]=1;
	}
	f[n+1]=n+1;
	while(st.size())
	{
		int mn=st.begin()->ff,cur=st.begin()->ss,sum;
		vector<int> vs;
		while(cur<=n&&a[cur]==mn)
		{
			st.erase(mp(mn,cur));
			vs.pb(cur);
			cur=gf(cur+1);
		}
		
		if((int)vs.size()<l)continue;
		cur=0;sum=0;
		for(int i=0;i<(int)vs.size();i++)
		{
			while(cur<(int)vs.size()&&i-cur+1>=l)sum+=lv[vs[cur++]];
			ans+=(LL)sum*rv[vs[i]];
		}
		int sz=(int)vs.size()/l;
		vector<int> nl,nr;
		for(int i=1;i<=sz;i++)
		{
			int lb=i*l-1,rb=min((i+1)*l-1,(int)vs.size()),s=0;
			for(int j=lb;j<rb;j++)s+=rv[vs[j]];
			nr.pb(s);
		}
		reverse(vs.begin(),vs.end());
		for(int i=1;i<=sz;i++)
		{
			int lb=i*l-1,rb=min((i+1)*l-1,(int)vs.size()),s=0;
			for(int j=lb;j<rb;j++)s+=lv[vs[j]];
			nl.pb(s);
		}
		reverse(nl.begin(),nl.end());
		reverse(vs.begin(),vs.end());
		
		for(int i=0;i<sz;i++)
		{
			lv[vs[i]]=nl[i];
			rv[vs[i]]=nr[i];
		}
		for(int i=sz;i<(int)vs.size();i++)f[vs[i]]=vs[i]+1;
		vs.resize(sz);
		
		cur=0;sum=0;
		for(int i=0;i<(int)vs.size();i++)
		{
			while(cur<(int)vs.size()&&i-cur+1>=l)sum+=lv[vs[cur++]];
			ans-=(LL)sum*rv[vs[i]];
		}
		for(int i=0;i<(int)vs.size();i++)
		{
			a[vs[i]]++;
			st.insert(mp(a[vs[i]],vs[i]));
		}
	}
	printf("%lld\n",ans+n);
	return 0;
}