#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar()) {
    if (ch == '-') f = -1;
  }
  for (; isdigit(ch); ch = getchar()) {
    x = x * 10 + ch - 48;
  }
  return x * f;
}
const int N = 2e5;
priority_queue<pair<int, int>, vector<pair<int, int> >,
               greater<pair<int, int> > >
    que;
int nxt[N + 3], prv[N + 3];
int a[N + 3];
long long cl[N + 3], cr[N + 3], cl2[N + 3], cr2[N + 3];
vector<int> vec;
int n, m;
long long ans;
void solve(int l, int r) {
  long long sum = 0ll;
  for (int i = m - 1; i < vec.size(); i++) {
    sum += cl[vec[i - m + 1]];
    ans += cr[vec[i]] * sum;
  }
  int cnt = (r - l + 1) / m;
  for (int i = l; i <= l + cnt - 1; i++) cl2[i] = cr2[i] = 0ll;
  for (int i = vec.size() - m, j = 0, k = l + cnt - 1; i >= 0; i--) {
    cl2[k] += cl[vec[i]];
    if (j == m - 1) {
      k--, j = 0;
    } else {
      j++;
    }
  }
  for (int i = m - 1, j = 0, k = l; i < vec.size(); i++) {
    cr2[k] += cr[vec[i]];
    if (j == m - 1) {
      k++, j = 0;
    } else {
      j++;
    }
  }
  for (int i = l; i <= l + cnt - 1; i++) cl[i] = cl2[i], cr[i] = cr2[i];
  sum = 0ll;
  for (int i = l + m - 1; i <= l + cnt - 1; i++) {
    sum += cl[i - m + 1];
    ans -= cr[i] * sum;
  }
  nxt[l + cnt - 1] = nxt[r];
  prv[nxt[r]] = l + cnt - 1;
  for (int i = l; i < l + cnt - 1; i++) nxt[i] = i + 1;
  for (int i = l + 1; i <= l + cnt - 1; i++) prv[i] = i - 1;
  int val = a[l];
  for (int i = l; i <= l + cnt - 1; i++) a[i] = val + 1;
  for (int i = l; i <= l + cnt - 1; i++) que.push(make_pair(a[i], i));
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), que.push(make_pair(a[i], i)),
        cl[i] = cr[i] = 1ll, nxt[i] = i + 1, prv[i] = i - 1;
  while (1) {
    int l = que.top().second, r = l;
    que.pop();
    vec.clear();
    vec.push_back(l);
    while (!que.empty() && que.top().first == a[l] &&
           que.top().second == nxt[r]) {
      r = nxt[r];
      vec.push_back(r);
      que.pop();
    }
    solve(l, r);
    if (prv[l] == 0 && nxt[r] == n + 1) break;
  }
  printf("%lld\n", ans + n);
  return 0;
}
