#include <bits/stdc++.h>
using namespace std;
const int maxn = 450000 + 100;
int n, k;
vector<int> pos[maxn];
int a[maxn];
int b[maxn];
int v[maxn];
int main() {
  while (cin >> n >> k) {
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      v[i] = a[i];
      pos[i].push_back(0);
    }
    pos[0].push_back(0);
    int tot = n;
    sort(v + 1, v + n + 1);
    tot = unique(v + 1, v + n + 1) - (v + 1);
    for (int i = 1; i <= n; i++) {
      a[i] = lower_bound(v + 1, v + 1 + tot, a[i]) - (v + 1) + 1;
    }
    long long mx = 0, ans = 0;
    for (int i = 1; i <= n; i++) {
      int v = a[i];
      pos[v].push_back(i);
      int sz = pos[v].size();
      while (b[v] + k < sz) {
        b[v]++;
      }
      mx = max(mx, (long long)pos[v][b[v]]);
      ans += mx;
    }
    cout << ans << endl;
  }
}
