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