#include <bits/stdc++.h>
using namespace std;
char command[105];
int dp[101][51][201][2];
int main() {
  int n, sz;
  scanf("%s%d", command, &n);
  sz = (int)strlen(command);
  for (int com = 0; com <= n; com++)
    for (int pos = 0; pos <= 200; pos++)
      for (int dir = 0; dir < 2; dir++)
        if (com % 2 == 0) dp[sz][com][pos][dir] = abs(100 - pos);
  for (int i = sz - 1; i >= 0; i--)
    for (int com = 0; com <= n; com++)
      for (int pos = 0; pos <= 200; pos++)
        for (int dir = 0; dir < 2; dir++) {
          int ret1 = 0;
          if (command[i] == 'T')
            ret1 = max(ret1, dp[i + 1][com][pos][!dir]);
          else if (command[i] == 'F' && dir == 0 && pos < 200)
            ret1 = max(ret1, dp[i + 1][com][pos + 1][dir]);
          else if (command[i] == 'F' && dir == 1 && pos)
            ret1 = max(ret1, dp[i + 1][com][pos - 1][dir]);
          if (!com) {
            dp[i][com][pos][dir] = ret1;
            continue;
          }
          int ret2 = 0;
          if (command[i] == 'T' && dir == 0 && pos < 200)
            ret2 = max(ret2, dp[i + 1][com - 1][pos + 1][dir]);
          else if (command[i] == 'T' && dir == 1 && pos)
            ret2 = max(ret2, dp[i + 1][com - 1][pos - 1][dir]);
          else if (command[i] == 'F')
            ret2 = max(ret2, dp[i + 1][com - 1][pos][!dir]);
          dp[i][com][pos][dir] = max(ret1, ret2);
        }
  printf("%d\n", dp[0][n][100][0]);
}
