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