#include <bits/stdc++.h>
using namespace std;
char s[105];
int n, k, dp[105][55][2], vis[105][55][2], sum[105];
int main() {
  scanf("%s", s + 1);
  scanf("%d", &k);
  n = strlen(s + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= k; j++) {
      dp[i][j][1] = 1000;
      dp[i][j][0] = -1000;
    }
  for (int i = 0; 2 * i <= k; i++) {
    if (s[n] == 'F') dp[n][2 * i][1] = dp[n][2 * i][0] = 1;
    if (s[n] == 'T') dp[n][2 * i][1] = dp[n][2 * i][0] = 0;
  }
  for (int i = 0; 2 * i + 1 <= k; i++) {
    if (s[n] == 'F') dp[n][2 * i + 1][1] = dp[n][2 * i + 1][0] = 0;
    if (s[n] == 'T') dp[n][2 * i + 1][1] = dp[n][2 * i + 1][0] = 1;
  }
  for (int i = n - 1; i >= 1; i--)
    for (int j = 0; j <= k; j++) {
      for (int l = 0; 2 * l <= j; l++) {
        if (s[i] == 'F') {
          dp[i][j][0] = max(1 + dp[i + 1][j - 2 * l][0], dp[i][j][0]);
          dp[i][j][1] = min(1 + dp[i + 1][j - 2 * l][1], dp[i][j][1]);
        }
        if (s[i] == 'T') {
          dp[i][j][0] = max(-dp[i + 1][j - 2 * l][1], dp[i][j][0]);
          dp[i][j][1] = min(-dp[i + 1][j - 2 * l][0], dp[i][j][1]);
        }
      }
      for (int l = 0; 2 * l + 1 <= j; l++) {
        if (s[i] == 'F') {
          dp[i][j][0] = max(-dp[i + 1][j - 2 * l - 1][1], dp[i][j][0]);
          dp[i][j][1] = min(-dp[i + 1][j - 2 * l - 1][0], dp[i][j][1]);
        }
        if (s[i] == 'T') {
          dp[i][j][0] = max(1 + dp[i + 1][j - 2 * l - 1][0], dp[i][j][0]);
          dp[i][j][1] = min(1 + dp[i + 1][j - 2 * l - 1][1], dp[i][j][1]);
        }
      }
    }
  printf("%d\n", max(dp[1][k][0], -dp[1][k][1]));
}
