#include <bits/stdc++.h>
using namespace std;
int dp[105][105][205][2];
string s1;
int main() {
  int n;
  int ans = 0;
  memset(dp, 0, sizeof(dp));
  cin >> s1;
  cin >> n;
  dp[0][0][100][0] = 1;
  for (int i = 1; i <= s1.size(); i++) {
    for (int j = 0; j <= 100; j++) {
      for (int k = 0; k <= 200; k++) {
        if (dp[i - 1][j][k][0] == 1) {
          if (s1[i - 1] == 'F') {
            for (int z = 0; z <= 50; z = z + 2) {
              if (j + z > n) break;
              dp[i][j + z][k + 1][0] = 1;
            }
            for (int z = 0; z <= 50; z = z + 2) {
              if (j + z + 1 > n) break;
              dp[i][j + 1 + z][k][1] = 1;
            }
          }
          if (s1[i - 1] == 'T') {
            for (int z = 0; z <= 50; z = z + 2) {
              if (j + z > n) break;
              dp[i][j + z][k][1] = 1;
            }
            for (int z = 0; z <= 50; z = z + 2) {
              if (j + z + 1 > n) break;
              dp[i][j + 1 + z][k + 1][0] = 1;
            }
          }
        }
        if (dp[i - 1][j][k][1] == 1) {
          if (s1[i - 1] == 'F') {
            dp[i][j][k - 1][1] = 1;
            dp[i][j + 1][k][0] = 1;
          }
          if (s1[i - 1] == 'T') {
            dp[i][j][k][0] = 1;
            dp[i][j + 1][k - 1][1] = 1;
          }
        }
      }
    }
  }
  for (int i = 0; i <= 201; i++) {
    if (dp[s1.size()][n][i][0] == 1) ans = max(ans, abs(i - 100));
    if (dp[s1.size()][n][i][1] == 1) ans = max(ans, abs(i - 100));
  }
  cout << ans << endl;
  return 0;
}
