#include <bits/stdc++.h>
using namespace std;
int main() {
  char str[200];
  int dp[200][200][2];
  int change, len;
  cin >> (str + 1);
  cin >> change;
  len = strlen(str + 1);
  int i, j;
  memset(dp, 0, sizeof(dp));
  for (i = 0; i <= change; i++) {
    if (str[1] == 'F') {
      dp[1][i][0] = i % 2 - 1;
      dp[1][i][1] = 1 - i % 2;
    } else {
      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 <= change; j++) {
      if (str[i] == 'F') {
        dp[i][j][0] =
            (dp[i - 1][j][0] - 1 > dp[i - 1][j - 1][1] ? dp[i - 1][j][0] - 1
                                                       : dp[i - 1][j - 1][1]);
        dp[i][j][1] =
            (dp[i - 1][j][1] + 1 > dp[i - 1][j - 1][0] ? dp[i - 1][j][1] + 1
                                                       : dp[i - 1][j - 1][0]);
      } else {
        dp[i][j][0] = (dp[i - 1][j][1] > dp[i - 1][j - 1][0] - 1
                           ? dp[i - 1][j][1]
                           : dp[i - 1][j - 1][0] - 1);
        dp[i][j][1] = (dp[i - 1][j][0] > dp[i - 1][j - 1][1] + 1
                           ? dp[i - 1][j][0]
                           : dp[i - 1][j - 1][1] + 1);
      }
    }
  }
  cout << (dp[len][change][0] > dp[len][change][1] ? dp[len][change][0]
                                                   : dp[len][change][1])
       << endl;
  return 0;
}
