#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;
template<typename S, typename T = S> void chmax(S &s, T t) {s = s > t ? s : t;}
const int N = 1e5 + 5;
const int K = 11;
const int inf = 1e9;
// int dp[N][K][K][3]; // i, 0: [a][b], 1: [a][c], 2: [b][c]
vector<vector<vector<vector<int>>>> dp(
N, vector<vector<vector<int>>>(K, vector<vector<int>>(K, vector<int>(3)))
);
int n, k;
void rm(int i, int a, int b, int c, int pre) {
assert(a > 0 && b > 0 && c > 0);
if (a == 1) {
int m = 2; // [b][c]
chmax(dp[i][b-1][c-1][m], pre + 1);
}
if (b == 1) {
int m = 1; // [a][c]
chmax(dp[i][a-1][c-1][m], pre + 1);
}
if (c == 1) {
int m = 0; // [a][b]
chmax(dp[i][a-1][b-1][m], pre + 1);
}
}
void m0(int i, int a, int b, char ch) {
int m = 0; // [a][b]
int pre = dp[i-1][a][b][m];
if (pre < 0)return; // previous state impossible
// always: (not taking current character)
chmax(dp[i][a][b][m], pre);
if (a + b == k) return; // can't take current character
if (ch == 'A') {
chmax(dp[i][a+1][b][m], pre);
return;
}
if (ch == 'B') {
chmax(dp[i][a][b+1][m], pre);
return;
}
// else ch == 'C'
int c = 1;
int mn = min({a, b, c});
if (mn > 0) {
rm(i, a, b, c, pre); // try removing different stuff
return;
}
// now either a == 0 or b == 0
assert(a == 0 || b == 0);
if (a == 0) {
m = 2; // [b][c]
chmax(dp[i][b][c][m], pre);
}
if (b == 0) {
m = 1; // [a][c]
chmax(dp[i][a][c][m], pre);
}
}
void m1(int i, int a, int c, char ch) {
int m = 1; // [a][c]
int pre = dp[i-1][a][c][m];
if (pre < 0)return; // previous state impossible
// always: (not taking current character)
chmax(dp[i][a][c][m], pre);
if (a + c == k) return; // can't take current character
if (ch == 'A') {
chmax(dp[i][a+1][c][m], pre);
return;
}
if (ch == 'C') {
chmax(dp[i][a][c+1][m], pre);
return;
}
// else ch == 'B'
int b = 1;
int mn = min({a, b, c});
if (mn > 0) {
rm(i, a, b, c, pre); // try removing different stuff
return;
}
// now either a == 0 or c == 0
assert(a == 0 || c == 0);
if (a == 0) {
m = 2; // [b][c]
chmax(dp[i][b][c][m], pre);
}
if (c == 0) {
m = 0; // [a][b]
chmax(dp[i][a][b][m], pre);
}
}
void m2(int i, int b, int c, char ch) {
int m = 2; // [b][c]
int pre = dp[i-1][b][c][m];
if (pre < 0)return; // previous state impossible
// always: (not taking current character)
chmax(dp[i][b][c][m], pre);
if (b + c == k) return; // can't take current character
if (ch == 'B') {
chmax(dp[i][b+1][c][m], pre);
return;
}
if (ch == 'C') {
chmax(dp[i][b][c+1][m], pre);
return;
}
// else ch == 'A'
int a = 1;
int mn = min({a, b, c});
if (mn > 0) {
rm(i, a, b, c, pre); // try removing different stuff
return;
}
// now either b == 0 or c == 0
assert(b == 0 || c == 0);
if (b == 0) {
m = 1; // [a][c]
chmax(dp[i][a][c][m], pre);
}
if (c == 0) {
m = 0; // [a][b]
chmax(dp[i][a][b][m], pre);
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
string s; cin >> s;
s = "#" + s;
for (int i = 0; i <= n; ++i) {
for (int a = 0; a <= k; ++a) {
for (int b = 0; a + b <= k; ++b) {
for (int m = 0; m < 3; ++m) {
dp[i][a][b][m] = -inf;
}
}
}
}
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
int ans = 0;
for (int i = 1; i <= n; ++i) {
char ch = s[i];
for (int e = 0; e <= k; ++e) {
for (int f = 0; e + f <= k; ++f) {
for (int m = 0; m < 3; ++m) {
m0(i, e, f, ch);
m1(i, e, f, ch);
m2(i, e, f, ch);
}
}
}
}
for (int i = 1; i <= n; ++i) {
for (int a = 0; a <= k; ++a) {
for (int b = 0; a + b <= k; ++b) {
for (int m = 0; m < 3; ++m) {
chmax(ans, dp[i][a][b][m]);
}
}
}
}
cout << ans << "\n";
}