#include <iostream>
#include <stdio.h>
#include <unordered_set>
#include <map>
#include <string>
#define USETI unordered_set<int>
#define CNT(c, val) c.count(val)
#define NSRT(c, val) c.insert(val)
#define PRTI(i) printf("%d", i)
#define PRT(s) printf(s)
#define LBRK printf("\n")
#define PAUSE getchar();getchar()
#define FORS(num, val, n) for(int num = val; num < n; num++)
#define FOR(num, n) for(int num = 0; num < n; num++)
#define SITUATION LBRK; PRT("dNumbers = "); PRTI(dNumbers); LBRK; PRT("Start and end = "); PRTI(start); PRT(" "); PRTI(end); LBRK; PRT("Ans = "); PRTI(ans); LBRK; PAUSE; PAUSE
typedef long long ll;
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
ll ans = 0;
scanf("%d %d", &n, &k);
int* nums = (int*)malloc(4*n);
USETI nn;
FOR(i, n)
{
scanf("%d",(nums + i));
NSRT(nn, *(nums + i));
//if(nn.size() <= k)
//{ ans++; }
}
throw exception("Example");
if(nn.size() <= k)
{
ll f = n;
ans = f * ((f + 1) / 2.0);
printf("%lld", ans);
free(nums);
return 0;
}
nn.clear();
map<int, int> m;
int start = 0;
int end = -1;
int* endP = nums + end;
int* startP = nums + start;
int dNumbers = 0;
//ll waste = 0;
bool wasteFlag = false;
int endPrev;
while (dNumbers <= k)
{
end++;
endP++;
if (m[*endP] == 0)
{ dNumbers++; }
//LBRK;
//PRT("add number "); PRTI(*(nums + end));
//LBRK;
m[*endP] += 1;
}
ans += (ll)(end - start);
//SITUATION;
while (start < n - k)
{
//LBRK;
//PRT("remove number "); PRTI(*(nums + start));
//LBRK;
m[*startP] -= 1;
if (m[*startP] == 0)
{
dNumbers--;
wasteFlag = false;
}
start++;
if (start >= n - k)
{
break;
}
startP++;
if(!wasteFlag)
{
endPrev = end;
end++;
endP++;
if (end >= n)
{
end = n;
endP = nums + n;
}
else
{
//LBRK;
//PRT("add number "); PRTI(*(nums + end));
//LBRK;
if (m[*endP] == 0)
{
dNumbers++;
}
m[*endP] += 1;
}
if (dNumbers > k)
{
while (end - start >= k + 1)
{
//LBRK;
//PRT("remove number "); PRTI(*(nums + end));
//LBRK;
m[*endP] -= 1;
if (m[*endP] == 0)
{
if (dNumbers == k + 1)
{
m[*endP] += 1;
break;
}
dNumbers--;
}
end--;
endP--;
/*
if (dNumbers <= k)
{
end++;
endP++;
if (m[*endP] == 0)
{
dNumbers++;
}
else
{
PRT("x"); return 0;
}
//LBRK;
//PRT("add number "); PRTI(*(nums + end));
//LBRK;
m[*endP] += 1;
break;
}
*/
}
}
while (dNumbers <= k)
{
end++;
endP++;
if (end >= n)
{
end = n;
break;
}
if (m[*endP] == 0)
{
dNumbers++;
}
m[*endP] += 1;
}
}
if(!wasteFlag && endPrev == end)
{ wasteFlag = true; }
ans += (ll)(end - start);
//SITUATION;
}
ll lk = k;
ans += lk * ((lk + 1) / 2.0);
printf("%lld", ans);
free(nums);
/*
LBRK;
PRT("Wasted move "); printf("%lld", waste);
PAUSE;
*/
return 0;
}