#include <bits/stdc++.h>
using namespace std;
const int MAX_L = 100;
const int MAX_N = 50;
int N;
char buf[MAX_L + 1];
bool live[MAX_L + 1][2 * MAX_L + 1][MAX_N + 1][2];
int solve() {
  const int L = strlen(buf);
  live[0][MAX_L][N][0] = true;
  for (int i = 0; i < L; i++) {
    for (int p = 0; p <= 2 * MAX_L; p++) {
      for (int res = 0; res <= N; res++) {
        for (int d = 0; d < 2; d++)
          if (live[i][p][res][d]) {
            if (res > 0) {
              if (buf[i] == 'T') {
                live[i + 1][p + (d == 0 ? 1 : -1)][res - 1][d] = true;
              } else {
                live[i + 1][p][res - 1][1 - d] = true;
              }
            }
            if (buf[i] == 'T') {
              live[i + 1][p][res][1 - d] = true;
            } else {
              live[i + 1][p + (d == 0 ? 1 : -1)][res][d] = true;
            }
          }
      }
    }
  }
  int ans = 0;
  for (int i = 0; i <= 2 * MAX_L; i++) {
    for (int k = 0; k <= N; k += 2) {
      for (int j = 0; j < 2; j++) {
        if (live[L][i][k][j]) {
          ans = max(ans, abs(100 - i));
        }
      }
    }
  }
  return ans;
}
int main() {
  scanf("%[^\n]%*c", buf);
  scanf("%d", &N);
  printf("%d\n", solve());
  return 0;
}
