#include <bits/stdc++.h>
char s[128];
int dp[128][64][256][2];
int n, m;
int main() {
  scanf("%s", s);
  n = strlen(s);
  scanf("%d", &m);
  memset(dp, 0xff, sizeof(dp));
  dp[0][0][128][0] = 0;
  int i, j, k, l;
  for (i = 0; i <= n; i++)
    for (j = 0; j <= m; j++)
      for (k = 128 - i; k <= 128 + i; k++)
        for (l = 0; l < 2; l++)
          if (dp[i][j][k][l] >= 0) {
            if (s[i] == 'F') {
              int pos;
              if (l)
                pos = k + 1;
              else
                pos = k - 1;
              int ret = dp[i][j][k][l];
              if (abs(pos - 128) > ret) ret = abs(pos - 128);
              if (ret > dp[i + 1][j][pos][l]) dp[i + 1][j][pos][l] = ret;
              if (j < m) {
                if (dp[i][j][k][l] > dp[i + 1][j + 1][k][!l])
                  dp[i + 1][j + 1][k][!l] = dp[i][j][k][l];
              }
            } else {
              if (dp[i][j][k][l] > dp[i + 1][j][k][!l])
                dp[i + 1][j][k][!l] = dp[i][j][k][l];
              if (j < m) {
                int pos;
                if (l)
                  pos = k + 1;
                else
                  pos = k - 1;
                int ret = dp[i][j][k][l];
                if (abs(pos - 128) > ret) ret = abs(pos - 128);
                if (ret > dp[i + 1][j + 1][pos][l])
                  dp[i + 1][j + 1][pos][l] = ret;
              }
            }
          }
  int ret = 0;
  for (i = m % 2; i <= m; i += 2)
    for (j = 128 - n; j <= 128 + n; j++)
      for (k = 0; k < 2; k++)
        if (dp[n][i][j][k] >= 0 && abs(j - 128) > ret) ret = abs(j - 128);
  printf("%d\n", ret);
  return 0;
}
