#include <bits/stdc++.h>
using namespace std;
bool dp[111][222][55][2];
int main(void) {
  int i, j, l, n, k, res = 0, f;
  int p1[1111];
  char s[1111];
  gets(s);
  n = strlen(s);
  scanf(" %d", &k);
  memset(dp, 0, sizeof(dp));
  p1[0] = 1;
  for (i = 1; i < 111; i++) p1[i] = -p1[i - 1];
  dp[0][111][0][0] = 1;
  for (i = 1; i <= n; i++) {
    if (s[i - 1] == 'F') {
      for (j = -105; j <= 105; j++)
        for (l = 0; l <= k; l++)
          for (f = 0; f < 2; f++) {
            dp[i][111 + j + p1[f]][l][f] |= dp[i - 1][111 + j][l][f];
            dp[i][111 + j][l + 1][1 - f] |= dp[i - 1][111 + j][l][f];
          }
    } else
      for (j = -105; j <= 105; j++)
        for (l = 0; l <= k; l++)
          for (f = 0; f < 2; f++) {
            dp[i][111 + j][l][f] |= dp[i - 1][111 + j][l][1 - f];
            dp[i][111 + j + p1[f]][l + 1][f] |= dp[i - 1][111 + j][l][f];
          }
  }
  for (j = 0; j <= 105; j++)
    for (l = k; l >= 0; l -= 2)
      for (f = 0; f < 2; f++)
        if (dp[n][111 + j][l][f] || dp[n][111 - j][l][f]) res = abs(j);
  printf("%d\n", res);
  return 0;
}
