#include <bits/stdc++.h>
using namespace std;
const int maxv = 1000000;
char ch[1000];
int n;
int dpRight[110][51][2];
int dpLeft[110][51][2];
int main() {
  cin >> ch >> n;
  int len = strlen(ch);
  for (int i = 0; i < 110; i++)
    for (int j = 0; j < 51; j++)
      for (int k = 0; k < 2; k++) {
        dpRight[i][j][k] = -maxv;
        dpLeft[i][j][k] = maxv;
      }
  for (int j = 0; j <= n; j++) {
    for (int k = 0; k < 1; k++) {
      if (ch[0] == 'F') {
        {
          dpRight[0][j][k] = ((j % 2 == 0) ? 1 : 0);
          dpLeft[0][j][k] = ((j % 2 == 0) ? 1 : 0);
        }
        {
          dpRight[0][j][(k + 1) % 2] =
              ((j % 2 == 1) ? 0 : dpRight[0][j][(k + 1) % 2]);
          dpLeft[0][j][(k + 1) % 2] =
              ((j % 2 == 1) ? 0 : dpLeft[0][j][(k + 1) % 2]);
        }
      } else {
        dpRight[0][j][(k + 1) % 2] = 0;
        dpLeft[0][j][(k + 1) % 2] = 0;
        dpRight[0][j][k] = ((j % 2 == 1) ? 1 : 0);
        dpLeft[0][j][k] = ((j % 2 == 1) ? 1 : 0);
      }
    }
  }
  for (int i = 1; i < len; i++)
    for (int j = 0; j <= n; j++)
      for (int k = 0; k < 2; k++) {
        for (int kj = ((ch[i] == 'F') ? 0 : 1); j - kj >= 0; kj += 2) {
          dpRight[i][j][k] =
              std::max(dpRight[i][j][k],
                       dpRight[i - 1][j - kj][k] + ((k == 0) ? +1 : -1));
          dpLeft[i][j][k] = std::min(
              dpLeft[i][j][k], dpLeft[i - 1][j - kj][k] + ((k == 0) ? +1 : -1));
        }
        for (int kj = ((ch[i] == 'F') ? 1 : 0); j - kj >= 0; kj += 2) {
          dpRight[i][j][(k + 1) % 2] =
              std::max(dpRight[i][j][(k + 1) % 2], dpRight[i - 1][j - kj][k]);
          dpLeft[i][j][(k + 1) % 2] =
              std::min(dpLeft[i][j][(k + 1) % 2], dpLeft[i - 1][j - kj][k]);
        }
      }
  int res = 0;
  for (int i = 0; i < 2; i++) {
    res = std::max(res, abs(dpRight[len - 1][n][i]));
    res = std::max(res, abs(dpLeft[len - 1][n][i]));
  }
  cout << res << endl;
  return 0;
}
