// same two pointer original template optimal logic is use
// just change with respect to question
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, k;
string s;
cin >> n >> k >> s;
// Store each character and its frequency in the window
unordered_map<char, int> b;
int i = 0; // Left pointer of the window
int maxi = -1; // Longest valid substring length
// j is the right pointer of the window
for (int j = 0; j < n; j++)
{
// Add the current character to the window
b[s[j]] = b[s[j]] + 1;
// If distinct characters exceed k, i mean greater than k
// shrink the window from the left
while (b.size() > k) // in two pointer template sum > k same here we put with respect to question
{
b[s[i]] = b[s[i]] - 1; // remove that element // same two pointer template sum = sum - b[i]
// Remove the character from the map
// if its frequency becomes zero
if (b[s[i]] == 0)
{
b.erase(s[i]);
}
// Move the left pointer forward
i++;
}
// Check if the window has exactly k distinct characters then it will come directly but if the window is greater than only it will go in while loop
if (b.size() == k)
{
// here calculating the maximum window length
maxi = max(maxi, j - i + 1);
}
}
// Print the longest valid substring length
cout << maxi << endl;
return 0;
}