#include <bits/stdc++.h>
using namespace std;
const int MAN = 110;
const int MAM = 60;
int dp[MAN][MAM][2];
int INF = -100000000;
int main() {
  string str;
  int m;
  while (cin >> str >> m) {
    int len = str.size();
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i <= len; ++i) {
      for (int j = 0; j <= m; ++j) {
        dp[i][j][0] = INF;
        dp[i][j][1] = INF;
      }
    }
    dp[0][0][0] = 0;
    dp[0][0][1] = 0;
    for (int i = 1; i <= len; ++i) {
      for (int j = 0; j <= m; ++j) {
        if (str[i - 1] == 'F') {
          dp[i][j][0] = max(dp[i - 1][j][0] != INF ? dp[i - 1][j][0] + 1 : INF,
                            j > 0 ? dp[i - 1][j - 1][1] : INF);
          dp[i][j][1] = max(dp[i - 1][j][1] != INF ? dp[i - 1][j][1] - 1 : INF,
                            j > 0 ? dp[i - 1][j - 1][0] : INF);
        } else {
          dp[i][j][0] =
              max(dp[i - 1][j][1], (j > 0 && dp[i - 1][j - 1][0] != INF)
                                       ? dp[i - 1][j - 1][0] + 1
                                       : INF);
          dp[i][j][1] =
              max(dp[i - 1][j][0], (j > 0 && dp[i - 1][j - 1][1] != INF)
                                       ? dp[i - 1][j - 1][1] - 1
                                       : INF);
        }
      }
    }
    int ans = INF;
    for (int i = m; i >= 0; i -= 2) {
      ans = max(ans, max(dp[len][i][0], dp[len][i][1]));
    }
    cout << ans << endl;
  }
  return 0;
}
