#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 10;
struct node {
  int x, L, R, l, r;
};
bool operator<(node a, node b) {
  if (a.x == b.x) return a.l < b.l;
  return a.x < b.x;
}
set<node> s, s1;
vector<node> d;
int n, L;
long long ans;
int al[N], ar[N], q[N], pl[N], pr[N];
void calc(int x) {
  long long cnt = 0;
  for (int i = 0; i < d.size(); i++) {
    if (i >= L - 1) cnt += d[i - L + 1].L;
    ans += cnt * d[i].R;
  }
  int c1 = 1, t = 0, t1, L1 = 0;
  L1 = d[0].l;
  for (int i = 0; i < d.size(); i++) {
    if (c1 == L) {
      ar[++t] = 0;
      pl[t] = 1e9;
      pr[t] = 0;
      c1 = 0;
    }
    ar[t] += d[i].R;
    pl[t] = min(pl[t], d[i].l);
    pr[t] = max(pr[t], d[i].r);
    c1++;
  }
  t1 = t;
  c1 = 1, t++;
  for (int i = d.size() - 1; i >= 0; i--) {
    if (c1 == L) {
      al[--t] = 0;
      c1 = 0;
    }
    al[t] += d[i].L;
    c1++;
  }
  d.clear();
  pl[1] = L1;
  for (int i = 1; i <= t1; i++) {
    d.push_back((node){x + 1, al[i], ar[i], pl[i], pr[i]});
  }
  cnt = 0;
  for (int i = 0; i < d.size(); i++) {
    if (i >= L - 1) cnt += d[i - L + 1].L;
    ans -= cnt * d[i].R;
  }
}
int main() {
  cin >> n >> L;
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    s.insert((node){x, 1, 1, i, i});
  }
  while (s.size()) {
    s1.clear();
    int v = s.begin()->x;
    for (auto i : s) {
      if (!d.empty()) {
        if (d.back().r + 1 < i.l || i.x != v) {
          for (auto x : d) s.erase(x);
          if (d.size() < L) {
            d.clear();
          } else
            calc(v);
          for (auto x : d) s.insert(x);
          d.clear();
          break;
        }
      }
      if (i.x != v) break;
      d.push_back(i);
    }
    if (d.size() >= L) {
      calc(v);
    }
    if (d.size()) break;
  }
  cout << ans + n << endl;
}
