#include <iostream>
#include <map>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k, ans = 0;
scanf("%d %d", &n, &k);
map<int, int> m;
int* numbers = (int*)malloc(4 * n);
for(int i = 0; i < n; i++)
{
cin >> *(numbers + i);
//scanf("%d", (numbers + i));
}
if (k == 0)
{ printf("0"); return 0; }
if(k >= n)
{
printf("%d", (n*((n + 1.0) / 2)));
return 0;
}
int dAmount = 0;
for(int i = 0; i < n; i++)
{
int index = *(numbers + i);
if(m[index] == 0)
{ m[index] = 1; dAmount++; }
else
{ m[index] += 1; }
}
if (dAmount <= k)
{ ans = n * ((n + 1.0) / 2); }
else
{
int start = 0;
int end = n - 1;
bool goRight = false;
int growIndex, shrinkIndex;
int a, b;
int currentDNumbers = dAmount;
int d = currentDNumbers - k;
int min = INT_MAX;
if(d <= end - start - d)
{
for(int k = 0; k < d; k++)
{
m[*(numbers + start)] -= 1;
if(m[*(numbers + start)] == 0)
{ currentDNumbers--; }
start++;
}
}
else
{
start += d;
m.clear();
currentDNumbers = 0;
for(int k = 0; k < (end-start + 1); k++)
{
if(m[*(numbers + start + k)] == 0)
{ m[*(numbers + start + k)] = 1; currentDNumbers++; }
else {
m[*(numbers + start + k)] += 1;
}
}
}
while(end - start > (k - 1))
{
growIndex = (goRight) ? end + 1 : start - 1;
if (growIndex > n - 1)
{
if (end - start == k)
{ break; }
d = min - k;
if (d <= 1)
{
d = 1;
m[*(numbers + start)] -= 1;
if (m[*(numbers + start)] == 0)
{ currentDNumbers--; }
if (currentDNumbers <= k)
{ ans++; }
start++;
goRight = !goRight;
}
else if (d <= end - start - d)
{
for (int k = 0; k < d; k++)
{
m[*(numbers + start)] -= 1;
if (m[*(numbers + start)] == 0)
{ currentDNumbers--; }
start++;
}
goRight = !goRight;
}
else
{
start += d;
m.clear();
currentDNumbers = 0;
for (int k = 0; k < (end - start + 1); k++)
{
if (m[*(numbers + start + k)] == 0)
{ m[*(numbers + start + k)] = 1; currentDNumbers++; }
else
{ m[*(numbers + start + k)] += 1; }
}
goRight = !goRight;
}
if (currentDNumbers < min)
{ min = currentDNumbers; }
}
else if(growIndex < 0)
{
if (end - start == k)
{ break; }
d = min - k;
if (d <= 1)
{
d = 1;
m[*(numbers + end)] -= 1;
if (m[*(numbers + end)] == 0)
{ currentDNumbers--; }
if (currentDNumbers <= k)
{ ans++; }
end--;
goRight = !goRight;
}
else if (d <= end - start - d)
{
for (int k = 0; k < d; k++)
{
m[*(numbers + end)] -= 1;
if (m[*(numbers + end)] == 0)
{ currentDNumbers--; }
end--;
}
goRight = !goRight;
}
else
{
end -= d;
m.clear();
currentDNumbers = 0;
for (int k = end - start; k > -1; k++)
{
if (m[*(numbers + end + k)] == 0)
{ m[*(numbers + end + k)] = 1; currentDNumbers++; }
else
{ m[*(numbers + end + k)] += 1; }
}
goRight = !goRight;
}
}
else
{
shrinkIndex = (goRight) ? start : end;
a = m[*(numbers + shrinkIndex)];
b = m[*(numbers + growIndex)];
m[*(numbers + shrinkIndex)] -= 1;
m[*(numbers + growIndex)] += 1;
if (m[*(numbers + shrinkIndex)] == 0 && m[*(numbers + shrinkIndex)] != a)
{ currentDNumbers--; }
if (m[*(numbers + growIndex)] == 1 && m[*(numbers + growIndex)] != b)
{ currentDNumbers++; }
if (currentDNumbers <= k)
{ ans++; }
start += (goRight) ? 1 : -1;
end += (goRight) ? 1 : -1;
if(currentDNumbers < min)
{ min = currentDNumbers; }
}
}
ans += ((2 * n + 1)*k - k * k) / 2;
}
printf("%d", ans);
m.clear();
free(numbers);
return 0;
}