#include <bits/stdc++.h>
using namespace std;
char s[105];
int n, m, ans, f[105][55][2];
int main() {
  scanf("%s%d", s, &m);
  n = strlen(s);
  memset(f, 200, sizeof(f));
  f[0][0][0] = f[0][0][1] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j <= m; j++)
      for (int k = 0; k <= 1; k++) {
        if (f[i][j][k] + (k ? 1 : -1) > f[i + 1][j + (s[i] != 'F')][k])
          f[i + 1][j + (s[i] != 'F')][k] = f[i][j][k] + (k ? 1 : -1);
        if (f[i][j][k] > f[i + 1][j + (s[i] != 'T')][!k])
          f[i + 1][j + (s[i] != 'T')][!k] = f[i][j][k];
      }
  for (int i = m; i >= 0; i -= 2) ans = max(ans, max(f[n][i][0], f[n][i][1]));
  printf("%d\n", ans);
}
