#include <bits/stdc++.h>

using namespace std;

int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  long long ans = n;
  vector<int> l(n), r(n);
  set<pair<int, int>> st;
  set<int> cur;
  for (int i = 0; i < n; ++i) {
    l[i] = r[i] = 1;
    st.emplace(a[i], i);
    cur.insert(i);
  }
  while (!st.empty()) {
    int p = st.begin()->second;
    st.erase(st.begin());
    vector<int> pos(1, p);
    while (true) {
      auto it = cur.lower_bound(pos.back() + 1);
      if (it == cur.end() || a[*it] != a[p]) {
        break;
      }
      st.erase(make_pair(a[p], *it));
      pos.push_back(*it);
    }
    int s = pos.size();
    if (s >= m) {
      for (int p : pos) {
        cur.erase(p);
      }
      long long pref = 0;
      for (int i = m - 1; i < s; ++i) {
        pref += l[pos[i - m + 1]];
        ans += pref * r[pos[i]];
      }
      int new_s = s / m;
      vector<int> new_l(new_s), new_r(new_s);
      for (int i = 0; i < s; ++i) {
        int p = pos[i];
        if (i >= m - 1) {
          new_r[(i - m + 1) / m] += r[p];
        }
        if (i + m <= s) {
          new_l[new_s - 1 - (s - m - i) / m] += l[p];
        }
      }
      for (int i = 0; i < new_s; ++i) {
        int p = pos[i];
        l[p] = new_l[i]; r[p] = new_r[i];
        st.emplace(++a[p], p);
        cur.insert(p);
      }
      if (new_s >= m) {
        pref = 0;
        for (int i = m - 1; i < new_s; ++i) {
          pref += l[pos[i - m + 1]];
          ans -= pref * r[pos[i]];
        }
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
