#include <bits/stdc++.h>
using namespace std;
int i, j, x, n, m, QtieRu4, iConst = 100;
char s[128];
int hp[256][2][2];
bool f[128][64][256][2];
int getMin(int x, int y) {
  if (x < y)
    return x;
  else
    return y;
}
int main() {
  gets(s);
  scanf("%d", &n);
  m = strlen(s);
  memset(f, false, sizeof(f));
  for (x = 0; x <= 200; ++x)
    hp[x][0][0] = hp[x][0][1] = hp[x][1][0] = hp[x][1][1] = 27101992;
  for (j = 0; j <= n; ++j) {
    if (s[0] == 'F') {
      if (j % 2 == 0) {
        f[0][j][1 + iConst][1] = true;
        hp[1 + iConst][1][0] = j;
        hp[1 + iConst][1][0] = getMin(j, hp[1 + iConst][1][0]);
      }
      if (j % 2 == 1) {
        f[0][j][0 + iConst][0] = true;
        hp[0 + iConst][0][1] = j;
        hp[0 + iConst][0][1] = getMin(j, hp[0 + iConst][0][1]);
      }
    }
    if (s[0] == 'T') {
      if (j % 2 == 0) {
        f[0][j][0 + iConst][0] = true;
        hp[0 + iConst][0][0] = getMin(j, hp[0 + iConst][0][0]);
      }
      if (j % 2 == 1) {
        f[0][j][1 + iConst][1] = true;
        hp[1 + iConst][1][1] = getMin(j, hp[1 + iConst][1][1]);
      }
    }
  }
  for (i = 1; i < m; ++i)
    for (j = 0; j <= n; ++j)
      for (x = 0; x <= 200; ++x) {
        if (s[i] == 'F') {
          if (x > 0) f[i][j][x][1] |= f[i - 1][j][x - 1][1];
          if (x < 200) f[i][j][x][0] |= f[i - 1][j][x + 1][0];
          if (j > 0) {
            f[i][j][x][1] |= f[i - 1][j - 1][x][0];
            f[i][j][x][0] |= f[i - 1][j - 1][x][1];
          }
        }
        if (s[i] == 'T') {
          f[i][j][x][1] |= f[i - 1][j][x][0];
          f[i][j][x][0] |= f[i - 1][j][x][1];
          if (j > 0) {
            if (x < 200) f[i][j][x][0] |= f[i - 1][j - 1][x + 1][0];
            if (x > 0) f[i][j][x][1] |= f[i - 1][j - 1][x - 1][1];
          }
        }
        if (hp[x][0][j % 2] < j) f[i][j][x][0] = true;
        if (hp[x][1][j % 2] < j) f[i][j][x][1] = true;
        if (f[i][j][x][0]) hp[x][0][j % 2] = getMin(j, hp[x][0][j % 2]);
        if (f[i][j][x][1]) hp[x][1][j % 2] = getMin(j, hp[x][1][j % 2]);
      }
  QtieRu4 = 0;
  for (x = 200; x >= 0; --x)
    if (abs(x - 100) > QtieRu4)
      if (f[m - 1][n][x][0] || f[m - 1][n][x][1]) QtieRu4 = abs(x - 100);
  cout << QtieRu4;
  return 0;
}
