#include <bits/stdc++.h>
using namespace std;
int a[200002], b[200002], c[200002], d[200002], e[200002], f[200002], i, j, k,
    m, n, o[200002], p[200002], q, w;
long long x;
bool g[200002];
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]] = b[v], a[b[v]] = a[u];
  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);
}
