#include <bits/stdc++.h>
using namespace std;
char s[110];
int dp[110][60][210][2];
int main() {
  while (scanf("%s", s) == 1) {
    int n;
    scanf("%d", &n);
    memset(dp, 0, sizeof(dp));
    dp[0][0][100][0] = 1;
    int len = strlen(s);
    for (int i = 0; i < len; i++)
      for (int j = 0; j <= n; j++)
        for (int k = 0; k <= 200; k++)
          for (int l = 0; l < 2; l++) {
            if (dp[i][j][k][l] == 0) continue;
            int dx;
            if (s[i] == 'F') {
              if (l == 0)
                dx = 1;
              else
                dx = -1;
              int tdis = k + dx;
              for (int tk = j; tk <= n; tk += 2) dp[i + 1][tk][tdis][l] = 1;
              for (int tk = j + 1; tk <= n; tk += 2)
                dp[i + 1][tk][k][1 - l] = 1;
            } else {
              for (int tk = j; tk <= n; tk += 2) dp[i + 1][tk][k][1 - l] = 1;
              if (l == 0)
                dx = 1;
              else
                dx = -1;
              int tdis = k + dx;
              for (int tk = j + 1; tk <= n; tk += 2) dp[i + 1][tk][tdis][l] = 1;
            }
          }
    int anss = 0;
    for (int i = 0; i <= 200; i++)
      for (int l = 0; l < 2; l++)
        if (dp[len][n][i][l]) anss = max(anss, abs(i - 100));
    printf("%d\n", anss);
  }
  return 0;
}
