#include <bits/stdc++.h>
using namespace std;
int D[200][60][2][2];
int bestMax(int a, int b) {
  if (a == 1000) return b;
  if (b == 1000) return a;
  return max(a, b);
}
int bestMin(int a, int b) {
  if (a == 1000) return b;
  if (b == 1000) return a;
  return min(a, b);
}
int main() {
  string s;
  cin >> s;
  int N;
  scanf("%d", &N);
  for (int i = 0; i < 200; i++)
    for (int j = 0; j < 60; j++)
      for (int k = 0; k < 2; k++) D[i][j][k][0] = D[i][j][k][1] = 1000;
  D[0][N][0][0] = D[0][N][0][1] = 0;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j <= N; j++) {
      for (int k = 0; k < 2; k++) {
        for (int way = 0; way < 2; way++) {
          if (D[i][j][k][way] == 1000) continue;
          int step = k == 0 ? 1 : -1;
          if (s[i] == 'T') {
            for (int z = 0; z <= j; z += 2) {
              D[i + 1][j - z][(k + 1) % 2][0] =
                  bestMin(D[i + 1][j - z][(k + 1) % 2][0], D[i][j][k][way]);
              D[i + 1][j - z][(k + 1) % 2][1] =
                  bestMax(D[i + 1][j - z][(k + 1) % 2][1], D[i][j][k][way]);
            }
            for (int z = 1; z <= j; z += 2) {
              D[i + 1][j - z][k][0] =
                  bestMin(D[i + 1][j - z][k][0], D[i][j][k][way] + step);
              D[i + 1][j - z][k][1] =
                  bestMax(D[i + 1][j - z][k][1], D[i][j][k][way] + step);
            }
          } else {
            for (int z = 1; z <= j; z += 2) {
              D[i + 1][j - z][(k + 1) % 2][0] =
                  bestMin(D[i + 1][j - z][(k + 1) % 2][0], D[i][j][k][way]);
              D[i + 1][j - z][(k + 1) % 2][1] =
                  bestMax(D[i + 1][j - z][(k + 1) % 2][1], D[i][j][k][way]);
            }
            for (int z = 0; z <= j; z += 2) {
              D[i + 1][j - z][k][0] =
                  bestMin(D[i + 1][j - z][k][0], D[i][j][k][way] + step);
              D[i + 1][j - z][k][1] =
                  bestMax(D[i + 1][j - z][k][1], D[i][j][k][way] + step);
            }
          }
        }
      }
    }
  }
  int result = 0;
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 2; j++)
      if (D[s.size()][0][i][j] != 1000)
        result = max(result, abs(D[s.size()][0][i][j]));
  printf("%d", result);
  return 0;
}
