#include <bits/stdc++.h>
using namespace std;
int main(void) {
  char str[200];
  int i, j, c, len;
  int dp[200][200][2];
  scanf("%s%d", str + 1, &c);
  len = strlen(str + 1);
  memset(dp, 0, sizeof(dp));
  if (str[1] == 'F') {
    for (i = 0; i <= c; i++) {
      dp[1][i][0] = i % 2 - 1;
      dp[1][i][1] = (i + 1) % 2;
    }
  } else {
    for (i = 0; i <= c; i++) {
      dp[1][i][0] = -(i % 2);
      dp[1][i][1] = i % 2;
    }
  }
  for (i = 2; i <= len; i++) {
    if (str[i] == 'F') {
      dp[i][0][0] = dp[i - 1][0][0] - 1;
      dp[i][0][1] = dp[i - 1][0][1] + 1;
    } else {
      dp[i][0][0] = dp[i - 1][0][1];
      dp[i][0][1] = dp[i - 1][0][0];
    }
    for (j = 1; j <= c; j++) {
      if (str[i] == 'F') {
        dp[i][j][0] = max(dp[i - 1][j][0] - 1, dp[i - 1][j - 1][1]);
        dp[i][j][1] = max(dp[i - 1][j][1] + 1, dp[i - 1][j - 1][0]);
      } else {
        dp[i][j][0] = max(dp[i - 1][j][1], dp[i - 1][j - 1][0] - 1);
        dp[i][j][1] = max(dp[i - 1][j][0], dp[i - 1][j - 1][1] + 1);
      }
    }
  }
  if (dp[len][c][0] > dp[len][c][1])
    printf("%d\n", dp[len][c][0]);
  else
    printf("%d\n", dp[len][c][1]);
  return 0;
}
