#include <bits/stdc++.h>
using namespace std;
int dp[2][60][160][2];
char s[110];
int n;
int main() {
  int i, j, k, l, m, le;
  while (scanf("%s", s) == 1) {
    scanf("%d", &n);
    le = strlen(s);
    for (i = 0; i <= n; i++)
      for (j = 0; j < 2; j++)
        for (m = 0; m <= le; m++) dp[0][i][m][j] = -1;
    dp[0][0][0][0] = 0;
    int cr = 0;
    for (i = 0; i < strlen(s); i++) {
      for (k = 0; k <= n; k++)
        for (j = 0; j < 2; j++)
          for (m = 0; m <= le; m++) dp[1 - cr][k][m][j] = -1;
      for (j = 0; j <= n; j++)
        for (k = 0; k < 2; k++)
          for (m = 0; m <= le; m++)
            if (dp[cr][j][m][k] != -1) {
              for (l = j; l <= n; l++) {
                char z = s[i];
                int nl, nk;
                if ((l - j) % 2 == 1) {
                  if (z == 'F')
                    z = 'T';
                  else
                    z = 'F';
                }
                if (z == 'T') {
                  nk = 1 - k;
                  dp[1 - cr][l][m][nk] = 0;
                } else {
                  nl = m;
                  if (k == 0) {
                    nl++;
                    dp[1 - cr][l][nl][k] = 0;
                  }
                  if (k == 1) {
                    nl--;
                    nk = 1;
                    if (nl < 0) {
                      nk = 0;
                      nl = -nl;
                    }
                    dp[1 - cr][l][nl][nk] = 0;
                  }
                }
              }
            }
      cr = 1 - cr;
    }
    int ans = -1;
    for (i = 0; i <= le; i++)
      for (k = 0; k < 2; k++)
        if (dp[cr][n][i][k] == 0) {
          ans = i;
        }
    printf("%d\n", ans);
  }
}
