#include <bits/stdc++.h>
using namespace std;
static const int INF = 10000000;
void do_dp(vector<vector<vector<int> > >& dp, const string& s, int N) {
  const int M = s.size();
  for (int i = 0; i < M; i++) {
    if (s[i] == 'F') {
      for (int j = 0; j <= N; j++) {
        for (int k = 0; k <= j; k++) {
          if (k % 2 == 0) {
            dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j - k][0] + 1);
            dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j - k][1] - 1);
          } else {
            dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j - k][0]);
            dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j - k][1]);
          }
        }
      }
    } else {
      for (int j = 0; j <= N; j++) {
        for (int k = 0; k <= j; k++) {
          if (k % 2 == 0) {
            dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j - k][0]);
            dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j - k][1]);
          } else {
            dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j - k][0] + 1);
            dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j - k][1] - 1);
          }
        }
      }
    }
  }
}
int main() {
  string s;
  cin >> s;
  const int M = s.size();
  int N;
  cin >> N;
  vector<vector<vector<int> > > dp(
      M + 1, vector<vector<int> >(N + 1, vector<int>(2, -INF)));
  dp[0][0][0] = 0;
  do_dp(dp, s, N);
  int ans = max(dp[M][N][0], dp[M][N][1]);
  for (int i = 0; i <= M; i++) {
    for (int j = 0; j <= N; j++) {
      dp[i][j][0] = dp[i][j][1] = -INF;
    }
  }
  dp[0][0][1] = 0;
  do_dp(dp, s, N);
  ans = max(ans, max(dp[M][N][0], dp[M][N][1]));
  cout << ans << endl;
  return 0;
}
