#include<cstdio>
#include<algorithm>
#include<cctype>
using namespace std;
#define G getchar()
inline int read()
{
	int x=0; bool flg=false; char ch=G;
	for (;!isdigit(ch);ch=G) if (ch=='-') flg=true;
	for (;isdigit(ch);ch=G) x=(x<<3)+(x<<1)+(ch^48);
	return flg?-x:x;
}
#undef G
typedef long long ll;

int n,K;
int a[200010];
int p[200010];
bool cmp(int x,int y){
	if (a[x]^a[y]) return a[x]<a[y];
	return x<y;
}
struct Rec{
	int l,r,L,R;
}A[200010],B[200010]; int Atot,Btot;
ll ans;
int main()
{
	//freopen("data.in","r",stdin);
	//freopen("A.out","w",stdout);

	n=read(),K=read(); ans=n;
	for (int i=1;i<=n;i++) a[i]=read(),p[i]=i;
	sort(p+1,p+n+1,cmp);
	int pos=1,T;  //next considered; cur val
	while (1){
		if (!Atot){
			if (pos>n) break;
			T=a[p[pos]];
		}
		Btot=0;
		for (int i=1;i<=Atot;i++){
			while (pos<=n&&a[p[pos]]==T&&p[pos]<A[i].l)
				B[++Btot]=(Rec){p[pos],p[pos],1,1},pos++;
			B[++Btot]=A[i];
		}
		while (pos<=n&&a[p[pos]]==T)
			B[++Btot]=(Rec){p[pos],p[pos],1,1},pos++;
		Atot=Btot; for (int i=1;i<=Atot;i++) A[i]=B[i];
		for (int i=1,lst=-1,Ltot=0;i<=Atot;i++){
			if (i==1||A[i].l-1!=A[i-1].r) lst=i,Ltot=0;
			if (i-lst+1>=K) Ltot+=A[i-K+1].L;
			ans+=1LL*Ltot*A[i].R;
		}
		Btot=0;
		for (int i=1,j;i<=Atot;i=j+1){
			for (j=i;j+1<=Atot&&A[j].r+1==A[j+1].l;j++);
			if (j-i+1<K) continue;
			int t=(j-i+1)/K;
			for (int s=1,cur=K;s<=t;s++){
				B[++Btot]=(Rec){A[i].l+s-1,A[i].l+s-1,0,0};
				while (cur<=j-i+1&&cur/K==s){
					B[Btot].R+=A[i+cur-1].R;
					B[Btot].L+=A[j-cur+1].L;
					cur++;
				}
			}
			for (int a=Btot-t+1,b=Btot;a<b;swap(B[a++].L,B[b--].L));
			B[Btot].r=A[j].r;
		}
		Atot=Btot; for (int i=1;i<=Atot;i++) A[i]=B[i];
		for (int i=1,lst=-1,Ltot=0;i<=Atot;i++){
			if (i==1||A[i].l-1!=A[i-1].r) lst=i,Ltot=0;
			if (i-lst+1>=K) Ltot+=A[i-K+1].L;
			ans-=1LL*Ltot*A[i].R;
		}
		T++;
	}
	printf("%lld\n",ans);
	return 0;
}