#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void checkMax(T &a, T b) {
  if (a < b) a = b;
}
int const N = 128;
char cmd[N];
bitset<N * 2> dp[N][N][2];
int main() {
  while (scanf("%s", cmd + 1) == 1) {
    int m;
    scanf("%d", &m);
    int n = strlen(cmd + 1);
    dp[0][0][0].reset();
    dp[0][0][1].reset();
    dp[0][0][0][N] = 1;
    dp[0][0][1][N] = 1;
    for (int i = 1; i <= n; ++i) {
      for (int n_ = (m + 1), used = 0; used < n_; ++used) {
        if (cmd[i] == 'F') {
          dp[i][used][0] = dp[i - 1][used][0] << 1;
          dp[i][used][1] = dp[i - 1][used][1] >> 1;
        } else {
          dp[i][used][0] = dp[i - 1][used][1];
          dp[i][used][1] = dp[i - 1][used][0];
        }
        if (used) {
          if (cmd[i] == 'F') {
            dp[i][used][0] = dp[i][used][0] | dp[i - 1][used - 1][1];
            dp[i][used][1] = dp[i][used][1] | dp[i - 1][used - 1][0];
          } else {
            dp[i][used][0] = dp[i][used][0] | dp[i - 1][used - 1][0] << 1;
            dp[i][used][1] = dp[i][used][1] | dp[i - 1][used - 1][1] >> 1;
          }
        }
      }
    }
    int ans = 0;
    for (int n_ = (m + 1), used = 0; used < n_; ++used)
      if (used % 2 == m % 2) {
        for (int n_ = (N + N), i = 0; i < n_; ++i) {
          if (dp[n][used][0][i]) {
            checkMax<int>(ans, labs(i - N));
          }
        }
      }
    printf("%d\n", ans);
  }
}
