#include <bits/stdc++.h>
using namespace std;
bool dp[101][201][51][2] = {0};
int main() {
  string s;
  cin >> s;
  int n;
  cin >> n;
  for (int i = 0; i <= n; i += 2) dp[0][100][i][0] = 1;
  for (int i = 0; i < s.length(); i++)
    for (int j = 0; j < 201; j++) {
      for (int k = 0; k <= n; k++) {
        if (dp[i][j][k][0]) {
          if (s[i] == 'F')
            dp[i + 1][j + 1][k][0] = 1;
          else
            dp[i + 1][j][k][1] = 1;
        }
        if (dp[i][j][k][1]) {
          if (s[i] == 'F')
            dp[i + 1][j - 1][k][1] = 1;
          else
            dp[i + 1][j][k][0] = 1;
        }
      }
      for (int k = 0; k < n; k++) {
        if (dp[i][j][k][0]) {
          if (s[i] == 'F')
            dp[i + 1][j][k + 1][1] = 1;
          else
            dp[i + 1][j + 1][k + 1][0] = 1;
        }
        if (dp[i][j][k][1]) {
          if (s[i] == 'F')
            dp[i + 1][j][k + 1][0] = 1;
          else
            dp[i + 1][j - 1][k + 1][1] = 1;
        }
      }
    }
  int maxn = 0;
  for (int j = 0; j < 201; j++)
    for (int i = 0; i < 2; i++)
      if (dp[s.length()][j][n][i])
        if (abs(j - 100) > maxn) maxn = abs(j - 100);
  cout << maxn;
}
