#include <bits/stdc++.h>
using namespace std;
map<int, int> tp1;
set<int> tp3;
struct sth {
  int l, r, lb, rb;
  friend bool operator<(sth a, sth b) { return a.l < b.l; }
};
vector<sth> v[400600];
int su[400600][2], vl[400600][2], tp[400600][2], ct, n, k, s[400600], cnt;
long long as;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &s[i]);
    tp3.insert(s[i]);
    if (!tp1[s[i]]) tp1[s[i]] = ++cnt;
    v[tp1[s[i]]].push_back((sth){i, i, 1, 1});
  }
  int lb = *tp3.lower_bound(1);
  while (1) {
    int st = tp1[lb];
    sort(v[st].begin(), v[st].end());
    int sz = v[st].size(), las = v[st][0].l;
    for (int i = 0; i < sz; i++) {
      if (ct && v[st][i].l != v[st][i - 1].r + 1) {
        su[ct + 1][0] = su[ct + 1][1] = 0;
        int s1 = ct / k;
        if (s1) {
          for (int j = 1; j <= ct; j++) {
            long long r1 = j + k - 1, r2 = j + 1ll * k * k - 2;
            if (r1 <= ct && r2 > ct)
              as += 1ll * vl[j][1] * (su[ct][0] - su[r1 - 1][0]);
            else if (r1 <= ct)
              as += 1ll * vl[j][1] * (su[r2][0] - su[r1 - 1][0]);
          }
          for (int j = 1; j < s1; j++)
            tp[j][0] = su[(j + 1) * k - 1][0] - su[j * k - 1][0];
          tp[s1][0] = su[ct][0] - su[s1 * k - 1][0];
          for (int j = 1; j < s1; j++)
            tp[s1 - j + 1][1] =
                -su[ct - (j + 1) * k + 1][1] + su[ct - j * k + 1][1];
          tp[1][1] = su[ct - s1 * k + 1][1];
          if (!tp1[lb + 1]) tp1[lb + 1] = ++ct, tp3.insert(lb + 1);
          for (int j = 1; j <= s1; j++)
            v[tp1[lb + 1]].push_back(
                (sth){las + j - 1, j == s1 ? v[st][i - 1].r : las + j - 1,
                      tp[j][0], tp[j][1]});
        }
        ct = 0;
        las = v[st][i].l;
      }
      vl[++ct][0] = v[st][i].lb;
      vl[ct][1] = v[st][i].rb;
      su[ct][0] = su[ct - 1][0] + vl[ct][0];
      su[ct][1] = su[ct - 1][1] + vl[ct][1];
    }
    su[ct + 1][0] = su[ct + 1][1] = 0;
    int s1 = ct / k;
    if (s1) {
      for (int j = 1; j <= ct; j++) {
        long long r1 = j + k - 1, r2 = j + 1ll * k * k - 2;
        if (r1 <= ct && r2 > ct)
          as += 1ll * vl[j][1] * (su[ct][0] - su[r1 - 1][0]);
        else if (r1 <= ct)
          as += 1ll * vl[j][1] * (su[r2][0] - su[r1 - 1][0]);
      }
      for (int j = 1; j < s1; j++)
        tp[j][0] = su[(j + 1) * k - 1][0] - su[j * k - 1][0];
      tp[s1][0] = su[ct][0] - su[s1 * k - 1][0];
      for (int j = 1; j < s1; j++)
        tp[s1 - j + 1][1] =
            -su[ct - (j + 1) * k + 1][1] + su[ct - j * k + 1][1];
      tp[1][1] = su[ct - s1 * k + 1][1];
      if (!tp1[lb + 1]) tp1[lb + 1] = ++cnt, tp3.insert(lb + 1);
      for (int j = 1; j <= s1; j++)
        v[tp1[lb + 1]].push_back((sth){las + j - 1,
                                       j == s1 ? v[st][sz - 1].r : las + j - 1,
                                       tp[j][0], tp[j][1]});
    }
    ct = 0;
    set<int>::iterator it = tp3.lower_bound(lb + 1);
    if (it == tp3.end()) break;
    lb = *it;
  }
  printf("%lld\n", as + n);
}
