#include <bits/stdc++.h>
inline int Rand() { return (rand() << 16) | rand(); }
using namespace std;
const int N = 1001, SMALL = 105, ALPH = 28, BIG = 1000001;
int n, ans, cmp;
string s;
int dp[3][SMALL][SMALL][3];
bool was[3][SMALL][SMALL][3];
int add(int x, int where) {
  if (where == 0)
    return x + 1;
  else
    return x - 1;
}
int main() {
  srand(time(NULL));
  cin >> s;
  cin >> n;
  was[0][1][0][0] = was[0][1][0][1] = true;
  for (int i = 1; i <= s.size(); i++) {
    for (int swd = 0; swd <= n; swd++) {
      for (int dir = 0; dir <= 1; dir++) {
        if (!was[dir][i][swd][0]) continue;
        if (s[i - 1] == 'F') {
          for (int ret = swd; ret <= n; ret += 2) {
            if (!was[dir][i + 1][ret][0])
              dp[dir][i + 1][ret][0] = add(dp[dir][i][swd][0], dir),
                          dp[dir][i + 1][ret][1] = add(dp[dir][i][swd][1], dir);
            else
              dp[dir][i + 1][ret][0] =
                  min(dp[dir][i + 1][ret][0], add(dp[dir][i][swd][0], dir)),
                          dp[dir][i + 1][ret][1] =
                              max(dp[dir][i + 1][ret][1],
                                  add(dp[dir][i][swd][1], dir));
            was[dir][i + 1][ret][0] = was[dir][i + 1][ret][1] = true;
          }
          for (int ret = swd + 1; ret <= n; ret += 2) {
            if (!was[dir ^ 1][i + 1][ret][0])
              dp[dir ^ 1][i + 1][ret][0] = dp[dir][i][swd][0],
                              dp[dir ^ 1][i + 1][ret][1] = dp[dir][i][swd][1];
            else
              dp[dir ^ 1][i + 1][ret][0] =
                  min(dp[dir ^ 1][i + 1][ret][0], dp[dir][i][swd][0]),
                              dp[dir ^ 1][i + 1][ret][1] =
                                  max(dp[dir ^ 1][i + 1][ret][1],
                                      dp[dir][i][swd][1]);
            was[dir ^ 1][i + 1][ret][0] = was[dir ^ 1][i + 1][ret][1] = true;
          }
        } else {
          for (int ret = swd; ret <= n; ret += 2) {
            if (!was[dir ^ 1][i + 1][ret][0])
              dp[dir ^ 1][i + 1][ret][0] = dp[dir][i][swd][0],
                              dp[dir ^ 1][i + 1][ret][1] = dp[dir][i][swd][1];
            else
              dp[dir ^ 1][i + 1][ret][0] =
                  min(dp[dir ^ 1][i + 1][ret][0], dp[dir][i][swd][0]),
                              dp[dir ^ 1][i + 1][ret][1] =
                                  max(dp[dir ^ 1][i + 1][ret][1],
                                      dp[dir][i][swd][1]);
            was[dir ^ 1][i + 1][ret][0] = was[dir ^ 1][i + 1][ret][1] = true;
          }
          for (int ret = swd + 1; ret <= n; ret += 2) {
            if (!was[dir][i + 1][ret][0])
              dp[dir][i + 1][ret][0] = add(dp[dir][i][swd][0], dir),
                          dp[dir][i + 1][ret][1] = add(dp[dir][i][swd][1], dir);
            else
              dp[dir][i + 1][ret][0] =
                  min(dp[dir][i + 1][ret][0], add(dp[dir][i][swd][0], dir)),
                          dp[dir][i + 1][ret][1] =
                              max(dp[dir][i + 1][ret][1],
                                  add(dp[dir][i][swd][1], dir));
            was[dir][i + 1][ret][0] = was[dir][i + 1][ret][1] = true;
          }
        }
      }
    }
  }
  for (int dir = 0; dir <= 1; dir++)
    for (int mx = 0; mx <= 1; mx++)
      ans = max(ans, max(((dp[dir][((int)(s).size()) + 1][n][mx]) < 0
                              ? -(dp[dir][((int)(s).size()) + 1][n][mx])
                              : (dp[dir][((int)(s).size()) + 1][n][mx])),
                         ((dp[dir][((int)(s).size()) + 1][n][mx]) < 0
                              ? -(dp[dir][((int)(s).size()) + 1][n][mx])
                              : (dp[dir][((int)(s).size()) + 1][n][mx]))));
  cout << ans;
  return 0;
}
