#include <bits/stdc++.h>
using namespace std;
const int NS = 400100;
int n, k, top;
int a[NS], X[NS];
int b[NS];
vector<int> pos[NS];
int Fin(int key) {
  int l = 0, r = top - 1;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (X[mid] < key)
      l = mid + 1;
    else
      r = mid;
  }
  return r + 1;
}
int main() {
  while (~scanf("%d %d", &n, &k)) {
    top = 0;
    for (int i = 0; i <= n; i++) pos[i].clear(), pos[i].push_back(0), b[i] = 0;
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]), X[top++] = a[i];
    sort(X, X + top);
    top = unique(X, X + top) - X;
    for (int i = 1; i <= n; i++) a[i] = Fin(a[i]);
    long long ans = 0;
    int mx = 0;
    for (int i = 1; i <= n; i++) {
      int t = a[i];
      pos[t].push_back(i);
      int sz = pos[t].size();
      while (b[t] + k < sz) b[t]++;
      mx = max(mx, pos[t][b[t]]);
      ans += mx;
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
