#include <bits/stdc++.h>
using namespace std;
const int L = 105, N = 55;
int dp[L][N][2], sum[L], way[L];
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  string s;
  cin >> s;
  int n, l = s.size();
  cin >> n;
  int stt = 1, plc = 0;
  for (int i = 0; i < l; i++) {
    way[i] = stt;
    if (s[i] == 'F') {
      plc += stt;
    }
    if (s[i] == 'T') {
      stt *= -1;
    }
    sum[i + 1] = plc;
  }
  for (int i = 0; i <= l; i++) {
    dp[i][0][0] = dp[i][0][1] = sum[l];
  }
  for (int i = 0; i <= n; i += 2) {
    dp[l][i][0] = dp[l][i][0] = sum[l];
  }
  for (int i = l - 1; i >= 0; i--) {
    for (int j = 1; j <= n; j++) {
      if (l - i - 1 >= j) {
        dp[i][j][0] = dp[i + 1][j][0];
        dp[i][j][1] = dp[i + 1][j][1];
      } else {
        dp[i][j][0] = 1e9;
        dp[i][j][1] = -1e9;
      }
      if (s[i] == 'F') {
        dp[i][j][0] =
            min(dp[i][j][0], sum[i] * 2 - (dp[i + 1][j - 1][1] - way[i]));
        dp[i][j][1] =
            max(dp[i][j][1], sum[i] * 2 - (dp[i + 1][j - 1][0] - way[i]));
      }
      if (s[i] == 'T') {
        dp[i][j][0] =
            min(dp[i][j][0], sum[i] * 2 - dp[i + 1][j - 1][1] + way[i]);
        dp[i][j][1] =
            max(dp[i][j][1], sum[i] * 2 - dp[i + 1][j - 1][0] + way[i]);
      }
      if (j >= 2) {
        dp[i][j][0] = min(dp[i][j][0], dp[i][j - 2][0]);
        dp[i][j][1] = max(dp[i][j][1], dp[i][j - 2][1]);
      }
    }
  }
  cout << max(-dp[0][n][0], dp[0][n][1]);
  return 0;
}
