#include <bits/stdc++.h>
const int MaxN = 100;
using namespace std;
char str[MaxN + 10];
int n, dp[2][(MaxN << 1) + 20][MaxN + 20][52];
bool vst[2][(MaxN << 1) + 20][MaxN + 20][52];
int dfs(bool dir, int pos, int run, int change) {
  if (change < 0) return 0;
  if (!str[run]) return (change > 0) ? (0) : (abs(pos));
  if (vst[dir][pos + MaxN][run][change])
    return dp[dir][pos + MaxN][run][change];
  vst[dir][pos + MaxN][run][change] = true;
  return dp[dir][pos + MaxN][run][change] =
             max(dfs(dir, pos + (dir ? 1 : -1), run + 1,
                     change - (str[run] != 'F')),
                 dfs(!dir, pos, run + 1, change - (str[run] != 'T')));
}
int main(void) {
  int ans = 0;
  scanf("%s", str);
  scanf("%d", &n);
  for (; n >= 0; n -= 2) ans = max(ans, dfs(1, 0, 0, n));
  printf("%d\n", ans);
  return 0;
}
