#include <bits/stdc++.h>
using namespace std;
namespace io {
const int l = 1 << 20;
char buf[l], *s, *t, c;
inline void gc() {
  if (s == t) {
    t = (s = buf) + fread(buf, 1, l, stdin);
    c = s == t ? EOF : *s++;
  } else
    c = *s++;
}
template <class IT>
inline void gi(IT &x) {
  x = 0;
  gc();
  while (c < '0' || c > '9') gc();
  while ('0' <= c && c <= '9') {
    x = (x << 1) + (x << 3) + (c ^ 48);
    gc();
  }
}
char buf0[20];
int a;
template <class IT>
inline void pi(IT x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  do buf0[++a] = x % 10 + 48;
  while (x /= 10);
  while (a) putchar(buf0[a--]);
  putchar('\n');
}
};  // namespace io
using io::gi;
using io::pi;
template <class IT>
inline void cmax(IT &a, IT b) {
  if (a < b) a = b;
}
template <class IT>
inline void cmin(IT &a, IT b) {
  if (b < a) a = b;
}
const int N = 200005;
struct node {
  int a, L, R, l, r;
  inline bool operator<(const node &x) const {
    return a < x.a || (a == x.a && L < x.L);
  }
} z[N], x[N], y[N];
long long ans, ant;
inline void calcs(int i, int j, int r) {
  long long t = 0ll;
  for (; j <= r; ++i, ++j) ans += (t += x[i].l) * x[j].r;
}
inline void calct(int i, int j, int r) {
  long long t = 0ll;
  for (; j <= r; ++i, ++j) ant += (t += z[i].l) * z[j].r;
}
inline void dbg(int X) {
  int i;
  for (i = (1); i <= (X); ++i)
    printf("a=%d L=%d R=%d l=%d r=%d\n", x[i].a, x[i].L, x[i].R, x[i].l,
           x[i].r);
  puts("X");
}
int main() {
  int n, m, mn, X = 0, Y = 1, Z, T, i, j, k, l, c;
  scanf("%d%d", &n, &m);
  ans = n;
  for (i = (1); i <= (n); ++i) {
    scanf("%d", &y[i].a);
    y[i].L = y[i].R = i;
    y[i].l = y[i].r = 1;
  }
  sort(y + 1, y + n + 1);
  while (Y <= n) {
    mn = y[Y].a;
    if (X) cmin(mn, x[X].a);
    while (y[Y].a == mn) x[++X] = y[Y++];
    sort(x + 1, x + X + 1);
    x[X + 1].L = 0;
    Z = 0;
    for (i = j = 1; i <= X; i = ++j) {
      for (; x[j + 1].L == x[j].R + 1; ++j)
        ;
      c = (l = j + 1 - i) / m;
      if (c) {
        T = Z;
        for (k = (0); k < (c); ++k) {
          ++T;
          z[T].a = mn + 1;
          z[T].L = x[i + k * m].L;
          z[T].R = x[i + k * m + m - 1].R;
          z[T].l = z[T].r = 0;
        }
        z[T++].R = x[j].R;
        calcs(i, i + m - 1, j);
        for (k = (i + m - 1); k <= (j); ++k) z[Z + (k - i + 1) / m].r += x[k].r;
        for (k = (j - m + 1); k >= (i); --k) z[T - (j - k + 1) / m].l += x[k].l;
        calct(Z, Z + m - 1, --T);
        Z = T;
      }
    }
    X = Z;
    for (i = (1); i <= (X); ++i) x[i] = z[i];
  }
  printf("%lld", ans - ant);
  return 0;
}
