#include <bits/stdc++.h>
using namespace std;
const int MAXN = 110;
int N, len, ans;
bool dn[110][2][250][55];
char ar[MAXN];
void dfs(int loc, int dir, int dis, int left) {
  if (loc == len + 1) {
    if (!left) ans = max(ans, abs(dis));
    return;
  }
  if (dn[loc][dir][dis + 110][left]) return;
  dn[loc][dir][dis + 110][left] = true;
  if (ar[loc] == 'F')
    for (int i = 0; i <= left; i++)
      if (i % 2 == 0)
        dfs(loc + 1, dir, dir ? dis + 1 : dis - 1, left - i);
      else
        dfs(loc + 1, !dir, dis, left - i);
  if (ar[loc] == 'T')
    for (int i = 0; i <= left; i++)
      if (i % 2 == 0)
        dfs(loc + 1, !dir, dis, left - i);
      else
        dfs(loc + 1, dir, dir ? dis + 1 : dis - 1, left - i);
}
int main() {
  scanf(" %s %d", ar + 1, &N);
  len = strlen(ar + 1);
  dfs(1, 1, 0, N);
  printf("%d\n", ans);
  return 0;
}
