#include <bits/stdc++.h>
const int N = 110, MAX = 1 << 30, MIN = -(1 << 30);
char s[N];
int DP[N][N][2];
inline int max(int a, int b) { return a > b ? a : b; }
inline int min(int a, int b) { return a < b ? a : b; }
inline int abs(int A) { return A < 0 ? -A : A; }
void ini(int V) {
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) DP[i][j][0] = DP[i][j][1] = V;
}
int main() {
  int k;
  scanf("%s%d", &s[1], &k);
  int T = 0, n = strlen(&s[1]);
  for (int i = 1; i <= n; i++)
    if (s[i] == 'T') T++;
  if (T <= k) {
    printf("%d\n", n - ((k - T) % 2));
    return 0;
  }
  int ans = 0, V;
  V = MIN;
  ini(V);
  DP[1][0][0] = (s[1] == 'F' ? 1 : 0);
  DP[1][0][1] = (s[1] == 'F' ? V : 0);
  for (int i = 2; i <= n; i++)
    if (s[i] == 'F') {
      if (DP[i - 1][0][0] != V) DP[i][0][0] = DP[i - 1][0][0] + 1;
      if (DP[i - 1][0][1] != V) DP[i][0][1] = DP[i - 1][0][1] - 1;
    } else {
      DP[i][0][0] = DP[i - 1][0][1];
      DP[i][0][1] = DP[i - 1][0][0];
    }
  if (s[1] == 'F') {
    DP[1][1][0] = V;
    DP[1][1][1] = 0;
  } else {
    DP[1][1][0] = 1;
    DP[1][1][1] = V;
  }
  for (int i = 2; i <= n; i++)
    for (int j = 1; j <= i && j <= k; j++) {
      if (s[i] == 'F') {
        if (DP[i - 1][j][0] != V)
          DP[i][j][0] = max(DP[i - 1][j][0] + 1, DP[i - 1][j - 1][1]);
        else
          DP[i][j][0] = DP[i - 1][j - 1][1];
        if (DP[i - 1][j][1] != V)
          DP[i][j][1] = max(DP[i - 1][j][1] - 1, DP[i - 1][j - 1][0]);
        else
          DP[i][j][1] = DP[i - 1][j - 1][0];
      } else {
        if (DP[i - 1][j - 1][0] != V)
          DP[i][j][0] = max(DP[i - 1][j][1], DP[i - 1][j - 1][0] + 1);
        else
          DP[i][j][0] = DP[i - 1][j][1];
        if (DP[i - 1][j - 1][1] != V)
          DP[i][j][1] = max(DP[i - 1][j][0], DP[i - 1][j - 1][1] - 1);
        else
          DP[i][j][1] = DP[i - 1][j][0];
      }
    }
  if (DP[n][k][0] != V) ans = max(ans, abs(DP[n][k][0]));
  if (DP[n][k][1] != V) ans = max(ans, abs(DP[n][k][1]));
  V = MAX;
  ini(V);
  DP[1][0][0] = (s[1] == 'F' ? 1 : 0);
  DP[1][0][1] = (s[1] == 'F' ? V : 0);
  for (int i = 2; i <= n; i++)
    if (s[i] == 'F') {
      if (DP[i - 1][0][0] != V) DP[i][0][0] = DP[i - 1][0][0] + 1;
      if (DP[i - 1][0][1] != V) DP[i][0][1] = DP[i - 1][0][1] - 1;
    } else {
      DP[i][0][0] = DP[i - 1][0][1];
      DP[i][0][1] = DP[i - 1][0][0];
    }
  if (s[1] == 'F') {
    DP[1][1][0] = V;
    DP[1][1][1] = 0;
  } else {
    DP[1][1][0] = 1;
    DP[1][1][1] = V;
  }
  for (int i = 2; i <= n; i++)
    for (int j = 1; j <= i && j <= k; j++) {
      if (s[i] == 'F') {
        if (DP[i - 1][j][0] != V)
          DP[i][j][0] = min(DP[i - 1][j][0] + 1, DP[i - 1][j - 1][1]);
        else
          DP[i][j][0] = DP[i - 1][j - 1][1];
        if (DP[i - 1][j][1] != V)
          DP[i][j][1] = min(DP[i - 1][j][1] - 1, DP[i - 1][j - 1][0]);
        else
          DP[i][j][1] = DP[i - 1][j - 1][0];
      } else {
        if (DP[i - 1][j - 1][0] != V)
          DP[i][j][0] = min(DP[i - 1][j][1], DP[i - 1][j - 1][0] + 1);
        else
          DP[i][j][0] = DP[i - 1][j][1];
        if (DP[i - 1][j - 1][1] != V)
          DP[i][j][1] = min(DP[i - 1][j][0], DP[i - 1][j - 1][1] - 1);
        else
          DP[i][j][1] = DP[i - 1][j][0];
      }
    }
  if (DP[n][k][0] != V) ans = max(ans, abs(DP[n][k][0]));
  if (DP[n][k][1] != V) ans = max(ans, abs(DP[n][k][1]));
  printf("%d\n", ans);
  scanf(" ");
}
