#include <bits/stdc++.h>
using namespace std;
int a[1000010];
struct Val {
  int pos, val;
  Val(int a = 0, int b = 0) {
    pos = a;
    val = b;
  }
};
bool operator<(const Val& a, const Val& b) { return a.val > b.val; }
priority_queue<Val> q;
int pre[2000010], nxt[2000010];
int f[2000010], g[2000010];
int sum1[2000010];
int sum2[2000010];
bool vis[2000010];
int main() {
  long long ans = 0;
  int n, L;
  scanf("%d %d", &n, &L);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    f[i] = g[i] = 1;
    pre[i] = i - 1;
    nxt[i] = i + 1;
    q.push(Val(i, a[i]));
  }
  nxt[0] = 1;
  nxt[n] = 0;
  int tot = n;
  while (!q.empty()) {
    int pos = q.top().pos;
    q.pop();
    if (vis[pos]) continue;
    int lpos = pos, rpos = pos;
    while (a[pre[lpos]] == a[pos]) lpos = pre[lpos];
    while (a[nxt[rpos]] == a[pos]) rpos = nxt[rpos];
    int cnt = 0;
    int lst = 0;
    for (int i = lpos; a[i] == a[pos]; i = nxt[i]) {
      cnt++;
      if (i <= n) lst = i;
      sum1[cnt] = sum1[cnt - 1] + f[i];
      sum2[cnt] = sum2[cnt - 1] + g[i];
      vis[i] = true;
      if (cnt >= L) ans += (long long)g[i] * sum1[min(lst, cnt - L + 1)];
    }
    if (cnt < L) {
      nxt[pre[lpos]] = pre[nxt[rpos]] = 0;
      continue;
    }
    for (int i = 1; i < cnt / L; i++) {
      nxt[tot + i] = tot + i + 1;
      pre[tot + i + 1] = tot + i;
    }
    for (int i = 1; i <= cnt / L; i++) a[tot + i] = a[pos] + 1;
    nxt[pre[lpos]] = tot + 1;
    pre[tot + 1] = pre[lpos];
    pre[nxt[rpos]] = tot + cnt / L;
    nxt[tot + cnt / L] = nxt[rpos];
    for (int i = 1; i <= cnt / L; i++) {
      g[tot + i] = sum2[min((i + 1) * L - 1, cnt)] - sum2[i * L - 1];
      int ii = cnt / L - i + 1;
      f[tot + i] =
          sum2[cnt - ii * L + 1] - sum2[max(cnt - (ii + 1) * L + 1, 0)];
    }
    q.push(Val(tot + 1, a[tot + 1]));
    tot += cnt / L;
  }
  printf("%lld\n", ans + n);
  return 0;
}
