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