#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e6 + 5;
const int MOD = 1e9 + 7;
const int inf = 2e9;
int dp[105][105][2], k, n, ans;
string s;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  for (int i = 0; i < 105; i++) {
    for (int j = 0; j < 105; j++) {
      for (int k = 0; k < 2; k++) {
        dp[i][j][k] = -inf;
      }
    }
  }
  cin >> s >> k;
  n = s.size();
  if (s[0] == 'F') {
    dp[0][0][0] = 1;
    dp[0][1][1] = 0;
  } else {
    dp[0][0][1] = 0;
    dp[0][1][0] = 1;
  }
  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][1] = dp[i - 1][0][0];
      dp[i][0][0] = dp[i - 1][0][1];
    }
    for (int j = 1; j <= k; j++) {
      if (s[i] == 'F') {
        dp[i][j][0] = max(dp[i - 1][j][0] + 1, dp[i - 1][j - 1][1]);
        dp[i][j][1] = max(dp[i - 1][j][1] - 1, dp[i - 1][j - 1][0]);
      } 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 (j > 1) dp[i][j][0] = max(dp[i][j - 2][0], dp[i][j][0]);
      if (j > 1) dp[i][j][1] = max(dp[i][j - 2][1], dp[i][j][1]);
    }
  }
  ans = max(dp[n - 1][k][0], dp[n - 1][k][1]);
  for (int i = 0; i < 105; i++) {
    for (int j = 0; j < 105; j++) {
      for (int k = 0; k < 2; k++) {
        dp[i][j][k] = inf;
      }
    }
  }
  if (s[0] == 'F') {
    dp[0][0][0] = 1;
    dp[0][1][1] = 0;
  } else {
    dp[0][0][1] = 0;
    dp[0][1][0] = 1;
  }
  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][1] = dp[i - 1][0][0];
      dp[i][0][0] = dp[i - 1][0][1];
    }
    for (int j = 1; j <= k; j++) {
      if (s[i] == 'F') {
        dp[i][j][0] = min(dp[i - 1][j][0] + 1, dp[i - 1][j - 1][1]);
        dp[i][j][1] = min(dp[i - 1][j][1] - 1, dp[i - 1][j - 1][0]);
      } else {
        dp[i][j][0] = min(dp[i - 1][j - 1][0] + 1, dp[i - 1][j][1]);
        dp[i][j][1] = min(dp[i - 1][j - 1][1] - 1, dp[i - 1][j][0]);
      }
    }
  }
  int L = -min(dp[n - 1][k][0], dp[n - 1][k][1]);
  ans = max(ans, L);
  cout << ans;
}
