#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int n, m;
char str[N];
bool f[N][N / 2][N * 2][2];
int main() {
  scanf("%s%d", str + 1, &m), n = strlen(str + 1);
  f[0][0][N][0] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= m; j++)
      for (int k = 1; k < N + N - 1; k++)
        for (int u = 0; u < 2; u++)
          if (str[i] == 'T') {
            f[i][j][k][u] = f[i - 1][j][k][!u];
            if (j) f[i][j][k][u] |= f[i - 1][j - 1][k + (u ? 1 : -1)][u];
          } else {
            f[i][j][k][u] = f[i - 1][j][k + (u ? 1 : -1)][u];
            if (j) f[i][j][k][u] |= f[i - 1][j - 1][k][!u];
          }
  int res = 0;
  for (int i = 0; i < N * 2; i++)
    for (int j = 0; j < 2; j++)
      for (int k = m; k >= 0; k -= 2)
        if (f[n][k][i][j]) res = max(res, abs(i - N));
  printf("%d\n", res);
  return 0;
}
