#include <bits/stdc++.h>
using namespace std;
int N, K;
int id[500010], a[500010], f[500010];
long long ans;
bool cmp(int x, int y) { return a[x] < a[y] || (a[x] == a[y] && x < y); }
int main() {
  scanf("%d %d", &N, &K);
  for (int i = 1; i <= N; ++i) {
    scanf("%d", &a[i]);
    id[i] = i;
  }
  sort(id + 1, id + N + 1, cmp);
  for (int i = K; i <= N; ++i)
    if (a[id[i - K + 1]] == a[id[i]]) {
      f[id[i]] = id[i - K + 1];
    }
  for (int i = 1; i <= N; ++i) {
    f[i] = max(f[i], f[i - 1]);
    ans += f[i];
  }
  cout << ans;
  return 0;
}
