#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int64_t nn = 105;
const int64_t ii = 1e17;
int64_t dp[nn][nn][2 * nn][2];
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  for (int64_t i = 0; i < nn; i++)
    for (int64_t j = 0; j < nn; j++)
      for (int64_t k = 0; k < 2 * nn; k++)
        for (int64_t z = 0; z < 2; z++) dp[i][j][k][z] = -1;
  dp[0][0][nn][1] = 0;
  string s;
  cin >> s;
  int64_t n;
  cin >> n;
  for (int64_t i = 0; i < s.size(); i++) {
    for (int64_t j = 0; j <= n; j++) {
      for (int64_t r = 0; r < 2 * nn; r++) {
        for (int64_t z = 0; z < 2; z++) {
          if (dp[i][j][r][z] == -1) continue;
          if (s[i] == 'F') {
            if (z)
              dp[i + 1][j][r + 1][z] =
                  max(dp[i + 1][j][r + 1][z], abs((r + 1) - nn)),
                               dp[i + 1][j + 1][r][z ^ 1] = max(
                                   dp[i + 1][j + 1][r][z ^ 1], dp[i][j][r][z]);
            else
              dp[i + 1][j][r - 1][z] =
                  max(dp[i + 1][j][r - 1][z], abs((r - 1) - nn)),
                               dp[i + 1][j + 1][r][z ^ 1] = max(
                                   dp[i + 1][j + 1][r][z ^ 1], dp[i][j][r][z]);
          } else {
            if (z)
              dp[i + 1][j + 1][r + 1][z] =
                  max(dp[i + 1][j + 1][r + 1][z], abs((r + 1) - nn)),
                                   dp[i + 1][j][r][z ^ 1] = max(
                                       dp[i + 1][j][r][z ^ 1], dp[i][j][r][z]);
            else
              dp[i + 1][j + 1][r - 1][z] =
                  max(dp[i + 1][j + 1][r - 1][z], abs((r - 1) - nn)),
                                   dp[i + 1][j][r][z ^ 1] = max(
                                       dp[i + 1][j][r][z ^ 1], dp[i][j][r][z]);
          }
        }
      }
    }
  }
  int64_t ans = 0;
  for (int64_t i = 0; i <= n; i++) {
    for (int64_t r = 0; r < 2 * nn; r++) {
      for (int64_t z = 0; z < 2; z++)
        ans = max(ans, dp[s.size()][i][r][z] - (n - i) % 2);
    }
  }
  cout << ans;
}
