#include <bits/stdc++.h>
using namespace std;
char str[110];
int n, ans, len;
bool arr[110][220][55][2];
int main() {
  scanf("%s%d", str + 1, &n);
  len = strlen(str + 1);
  arr[0][100][0][0] = true;
  for (int i = 1; str[i]; ++i)
    for (int j = 100 - i; j <= 100 + i; ++j)
      for (int k = 0; k <= i && k <= n; ++k)
        for (int l = 0; l <= 1; ++l) {
          if (str[i] == 'T') {
            if (arr[i - 1][j][k][l ^ 1]) arr[i][j][k][l] = true;
            if (k > 0 && (arr[i - 1][j + ((l == 0) ? -1 : 1)][k - 1][l]))
              arr[i][j][k][l] = true;
          } else {
            if (arr[i - 1][j + ((l == 0) ? -1 : 1)][k][l])
              arr[i][j][k][l] = true;
            if (k > 0 && arr[i - 1][j][k - 1][l ^ 1]) arr[i][j][k][l] = true;
          }
        }
  for (int j = 100 - len; j <= 100 + len; ++j)
    for (int k = n; k >= 0; k -= 2)
      for (int l = 0; l <= 1; ++l)
        if (arr[len][j][k][l]) ans = max(ans, abs(100 - j));
  printf("%d\n", ans);
  return 0;
}
