#include <bits/stdc++.h>
using namespace std;
const int INFTY = 1 << 30;
string S;
int len;
int N;
int dp[105][55][2];
int main() {
  cin >> S >> N;
  len = S.size();
  for (int i = 0; i <= len; i++)
    for (int j = 0; j <= N; j++)
      for (int k = 0; k < 2; k++) dp[i][j][k] = -INFTY;
  dp[0][0][0] = dp[0][0][1] = 0;
  for (int i = 0; i < len; i++)
    for (int j = 0; j <= N; j++)
      for (int k = 0; k < 2; k++) {
        if (dp[i][j][k] == -INFTY) continue;
        if (S[i] == 'F') {
          dp[i + 1][j][k] =
              max(dp[i + 1][j][k], dp[i][j][k] + (k == 0 ? 1 : -1));
          dp[i + 1][j + 1][k ^ 1] = max(dp[i + 1][j + 1][k ^ 1], dp[i][j][k]);
        } else {
          dp[i + 1][j][k ^ 1] = max(dp[i + 1][j][k ^ 1], dp[i][j][k]);
          dp[i + 1][j + 1][k] =
              max(dp[i + 1][j + 1][k], dp[i][j][k] + (k == 0 ? 1 : -1));
        }
      }
  int ans = 0;
  for (int i = N; i >= 0; i -= 2)
    for (int j = 0; j < 2; j++)
      if (dp[len][i][j] != -INFTY) ans = max(ans, dp[len][i][j]);
  cout << ans << "\n";
  return 0;
}
