#include <bits/stdc++.h>
using namespace std;
int dp[110][110][2];
char s[110];
int main(void) {
  int k;
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  cin >> k;
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'F') {
      dp[i][0][0] = dp[i - 1][0][0] + 1;
      dp[i][0][1] = dp[i - 1][0][1] - 1;
    } else {
      dp[i][0][0] = dp[i - 1][0][1];
      dp[i][0][1] = dp[i - 1][0][0];
    }
    for (int j = 1; j < i && j <= k; j++) {
      if (s[i] == 'F') {
        dp[i][j][0] = max(dp[i - 1][j - 1][1], dp[i - 1][j][0] + 1);
        dp[i][j][1] = max(dp[i - 1][j - 1][0], dp[i - 1][j][1] - 1);
      } else {
        dp[i][j][0] = max(dp[i - 1][j - 1][0] + 1, dp[i - 1][j][1]);
        dp[i][j][1] = max(dp[i - 1][j - 1][1] - 1, dp[i - 1][j][0]);
      }
    }
    if (i <= k) {
      if (s[i] == 'F') {
        dp[i][i][0] = dp[i - 1][i - 1][1];
        dp[i][i][1] = dp[i - 1][i - 1][0];
      } else {
        dp[i][i][0] = dp[i - 1][i - 1][0] + 1;
        dp[i][i][1] = dp[i - 1][i - 1][1] - 1;
      }
    }
  }
  int ans = 0;
  for (int i = k; i >= 0; i -= 2) {
    ans = max(ans, dp[n][i][0]);
    ans = max(ans, dp[n][i][1]);
  }
  memset(dp, 0, sizeof(dp));
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'F') {
      dp[i][0][0] = dp[i - 1][0][0] - 1;
      dp[i][0][1] = dp[i - 1][0][1] + 1;
    } else {
      dp[i][0][0] = dp[i - 1][0][1];
      dp[i][0][1] = dp[i - 1][0][0];
    }
    for (int j = 1; j < i && j <= k; j++) {
      if (s[i] == 'F') {
        dp[i][j][0] = max(dp[i - 1][j - 1][1], dp[i - 1][j][0] - 1);
        dp[i][j][1] = max(dp[i - 1][j - 1][0], dp[i - 1][j][1] + 1);
      } else {
        dp[i][j][0] = max(dp[i - 1][j - 1][0] - 1, dp[i - 1][j][1]);
        dp[i][j][1] = max(dp[i - 1][j - 1][1] + 1, dp[i - 1][j][0]);
      }
    }
    if (i <= k) {
      if (s[i] == 'F') {
        dp[i][i][0] = dp[i - 1][i - 1][1];
        dp[i][i][1] = dp[i - 1][i - 1][0];
      } else {
        dp[i][i][0] = dp[i - 1][i - 1][0] - 1;
        dp[i][i][1] = dp[i - 1][i - 1][1] + 1;
      }
    }
  }
  for (int i = k; i >= 0; i -= 2) {
    ans = max(ans, dp[n][i][0]);
    ans = max(ans, dp[n][i][1]);
  }
  cout << ans << endl;
  return 0;
}
