#include <bits/stdc++.h>
using namespace std;
int lens, dp[105][105][2];
char com[105];
int brute(int in, int quo, int state) {
  int dir, temp, temp1;
  if (in >= lens) return 0;
  if (dp[in][quo][state] < 2000000000) return dp[in][quo][state];
  dir = (state == 0) ? -1 : 1;
  if (in == lens - 1) {
    if (((com[in] == 'T') && (quo % 2 == 1)) ||
        ((com[in] == 'F') && (quo % 2 == 0)))
      return dir;
    else
      return 0;
  }
  if (com[in] == 'F') return dir + brute(in + 1, quo, state);
  temp = brute(in + 1, quo, 1 - state);
  temp1 = (quo >= 1) ? (dir + brute(in + 1, quo - 1, state))
                     : brute(in + 1, quo, 1 - state);
  return max(temp, temp1);
}
int main() {
  int quo;
  scanf("%s", com);
  scanf("%d", &quo);
  lens = strlen(com);
  for (int i = lens - 1; i >= 0; i--) {
    for (int j = quo; j >= 0; j--) {
      dp[i][j][0] = 2000000000;
      dp[i][j][0] = brute(i, j, 0);
      dp[i][j][1] = 2000000000;
      dp[i][j][1] = brute(i, j, 1);
    }
  }
  printf("%d", max((int)abs(brute(0, quo, 1)), (int)abs(brute(0, quo, 0))));
}
