#include <bits/stdc++.h>
using namespace std;
const int mx = 108;
int dp[mx][56][2];
int n, m;
char s[mx];
int main() {
  gets(s + 1);
  scanf("%d", &n);
  int l = strlen(s + 1);
  int i, r = 1;
  memset(dp, 200, sizeof(dp));
  dp[1][0][1] = dp[1][0][0] = 0;
  for (int i = 1; i <= l + 1; i++)
    for (int j = 0; j <= n; j++)
      for (int d = 0; d < 2; d++) {
        if (s[i] == 'T') {
          dp[i + 1][j][!d] = max(dp[i + 1][j][!d], dp[i][j][d]);
          dp[i + 1][j + 1][d] =
              max(dp[i + 1][j + 1][d], dp[i][j][d] + (d ? 1 : -1));
        } else {
          dp[i + 1][j][d] = max(dp[i + 1][j][d], dp[i][j][d] + (d ? 1 : -1));
          dp[i + 1][j + 1][!d] = max(dp[i + 1][j + 1][!d], dp[i][j][d]);
        }
      }
  int ans = 0;
  for (int i = n; i >= 0; i -= 2) {
    ans = max(ans, dp[l + 1][i][0]);
    ans = max(ans, dp[l + 1][i][1]);
  }
  printf("%d\n", ans);
  return 0;
}
