#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long i, j, k, n, l, d;
  string s;
  cin >> s >> k;
  n = s.size();
  long long dp[n + 1][k + 1][201][2];
  memset(dp, 0, sizeof dp);
  dp[0][0][100][0] = 1;
  for (i = 1; i <= n; i++) {
    for (j = 0; j <= k; j++) {
      for (l = j; l <= k; l++) {
        for (d = 0; d <= 200; d++) {
          if (s[i - 1] == 'F') {
            if ((l - j) % 2 == 1) {
              dp[i][l][d][0] = dp[i][l][d][0] || dp[i - 1][j][d][1];
              dp[i][l][d][1] = dp[i][l][d][1] || dp[i - 1][j][d][0];
            } else {
              if (d != 0) {
                dp[i][l][d][0] = dp[i][l][d][0] || dp[i - 1][j][d - 1][0];
              }
              if (d != 200) {
                dp[i][l][d][1] = dp[i][l][d][1] || dp[i - 1][j][d + 1][1];
              }
            }
          } else {
            if ((l - j) % 2 == 0) {
              dp[i][l][d][0] = dp[i][l][d][0] || dp[i - 1][j][d][1];
              dp[i][l][d][1] = dp[i][l][d][1] || dp[i - 1][j][d][0];
            } else {
              if (d != 0) {
                dp[i][l][d][0] = dp[i][l][d][0] || dp[i - 1][j][d - 1][0];
              }
              if (d != 200) {
                dp[i][l][d][1] = dp[i][l][d][1] || dp[i - 1][j][d + 1][1];
              }
            }
          }
        }
      }
    }
  }
  long long ans = 0;
  for (d = 0; d <= 200; d++) {
    if (dp[n][k][d][0] || dp[n][k][d][1]) {
      ans = max(ans, abs(d - 100));
    }
  }
  cout << ans << "\n";
  return 0;
}
