#include<algorithm>
#include<cstdio>
#define N 200002
using namespace std;
int a[N],b[N],c[N],d[N],e[N],f[N],i,j,k,m,n,o[N],p[N],q,w;long long x;bool g[N];
inline bool cmp(int u,int v){return c[u]<c[v];}
inline void solve(int u,int v)
{
    for(;c[a[u]]==c[u];g[u=a[u]]=true);
    for(;c[b[v]]==c[v];g[v=b[v]]=true);
    for(j=v,q=0;u<=j;j=a[j])o[++q]=e[j];
    for(j=u,q=0;j<=v;j=b[j])p[++q]=f[j];
    for(j=q,o[q+1]=p[q+1]=0;j;j--)o[j]+=o[j+1],p[j]+=p[j+1];
    for(j=0;j<=q-m;j++)x+=(long long)(o[q-j]-o[q-j+1])*p[m+j];
    for(w=c[u];w!=c[a[u]]&&w!=c[b[v]]&&m<=q;q=j,w++)for(j=k=0;(k+=m)<=q;)o[++j]=o[k],p[j]=p[k];
    if(w!=c[a[u]]&&w!=c[b[v]])b[a[u]]=n+1,a[b[v]]=0;
    else
    {
        for(j=o[q+1]=p[q+1]=0;j<=q-m;j++)x-=(long long)(o[q-j]-o[q-j+1])*p[m+j];
        for(j=1,o[q+1]=p[q+1]=0;j<=q;j++)o[j]-=o[j+1],p[j]-=p[j+1];
        for(j=u,k=0;k<q;j=b[j])c[j]=w,e[j]=o[q-k],f[j]=p[++k];
        a[b[v]]=a[j],b[a[j]]=b[v];
    }
}
int main()
{
    scanf("%d%d",&n,&m),x=n;
    for(i=1;i<=n;i++)scanf("%d",c+i),d[i]=i,e[i]=f[i]=1,a[i]=i-1,b[i]=i+1;
    for(sort(d+1,d+n+1,cmp),i=1;i<=n;i++)if(!g[d[i]])solve(d[i],d[i]);
    return 0&printf("%lld\n",x);
}