#include <bits/stdc++.h>
using namespace std;
bool dp[110][110][110 << 1][2];
int solve(char str[110], int m) {
  int n = strlen(str);
  memset(dp, 0, sizeof(dp));
  dp[0][0][110][0] = true;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= m; j++) {
      for (int k = 0; k < 110 << 1; k++) {
        for (int d = 0; d < 2; d++) {
          if (!dp[i][j][k][d]) continue;
          if (str[i] == 'T') {
            dp[i + 1][j][k][d ^ 1] = true;
            dp[i + 1][j + 1][k + ((d == 1) ? 1 : -1)][d] = true;
          } else {
            dp[i + 1][j + 1][k][d ^ 1] = true;
            dp[i + 1][j][k + ((d == 1) ? 1 : -1)][d] = true;
          }
        }
      }
    }
  }
  int ans = 0;
  for (int i = m; i >= 0; i -= 2) {
    for (int k = 0; k < 110 << 1; k++) {
      for (int d = 0; d < 2; d++) {
        if (dp[n][i][k][d]) ans = max(ans, abs(k - 110));
      }
    }
  }
  return ans;
}
char str[110];
int main() {
  setvbuf(stdout, NULL, _IONBF, 0);
  int m;
  while (scanf(" %s %d", str, &m) == 2) {
    printf("%d", solve(str, m));
  }
  return 0;
}
