#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;
const int N = 1e5 + 5;
const int K = 11;
const int inf = 1e9;
int dp[N][K][K][K]; // i, a, b, c
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, k;
cin >> n >> k;
string s; cin >> s;
s = "#" + s;
if (k > 10) return 0;
for (int i = 0; i <= n; ++i) {
for (int a = 0; a <= k; ++a) {
for (int b = 0; b <= k; ++b) {
for (int c = 0; c <= k; ++c) {
dp[i][a][b][c] = -inf;
}
}
}
}
dp[0][0][0][0] = 0;
for (int i = 1; i <= n; ++i) {
char ch = s[i];
for (int a = 0; a <= k; ++a) {
for (int b = 0; a + b <= k; ++b) {
for (int c = 0; a + b + c <= k; ++c) {
assert(a + b + c <= k);
// a, b, c are from the previous
if (dp[i-1][a][b][c] < 0)continue;
int pre = dp[i-1][a][b][c];
dp[i][a][b][c] = max(dp[i][a][b][c], pre);
if (a + b + c == k) continue; // can't take more
int na = a + (int)(ch == 'A');
int nb = b + (int)(ch == 'B');
int nc = c + (int)(ch == 'C');
int mn = min({na, nb, nc});
if (mn > 0) {
dp[i][na-1][nb-1][nc-1] = max(dp[i][na-1][nb-1][nc-1], pre + 1);
}
dp[i][na][nb][nc] = max(dp[i][na][nb][nc], pre);
}
}
}
// cout << i << " " << ans << "\n";
}
int ans = 0;
for (int i = 0; i <= n; ++i) {
for (int a = 0; a <= k; ++a) {
for (int b = 0; b <= k; ++b) {
for (int c = 0; c <= k; ++c) {
if (a + b + c > k)continue;
ans = max(ans, dp[i][a][b][c]);
}
}
}
}
cout << ans << "\n";
}