#include <bits/stdc++.h>
using namespace std;
using namespace std;
using namespace std;
using namespace std;
using namespace std;
using namespace std;
using namespace std;
using namespace std;
using namespace std;
int dp[155][155][2];
string s;
int main() {
  int m, n, i, j, k, l;
  cin >> s >> m;
  int ans = 0;
  n = s.length();
  memset(dp, 222, sizeof(dp));
  dp[0][0][0] = dp[0][0][1] = 0;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m + 1; j++) {
      for (k = 0; k < 2; k++) {
        dp[i + 1][j + (s[i] != 'F')][k] =
            max(dp[i + 1][j + (s[i] != 'F')][k], dp[i][j][k] + (k ? 1 : -1));
        dp[i + 1][j + (s[i] != 'T')][!k] =
            max(dp[i + 1][j + (s[i] != 'T')][!k], dp[i][j][k]);
      }
    }
  }
  for (i = m; i >= 0; i -= 2) ans = max({ans, dp[n][i][0], dp[n][i][1]});
  cout << ans << endl;
}
