#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using std::max;
using std::min;
using std::string;
const int MAX_INT = 1e+9 + 10, MIN_INT = -MAX_INT;
const int MAXN = 100 + 10, MAXK = 50 + 10;
bool is_move[MAXN];
int dyn[2][2][MAXK], tmp[2][2][MAXK];
void recalc_move(int k, int last) {
  tmp[0][0][k] = min(tmp[0][0][k], dyn[0][0][last] - 1);
  tmp[0][1][k] = min(tmp[0][1][k], dyn[0][1][last] + 1);
  tmp[1][0][k] = max(tmp[1][0][k], dyn[1][0][last] - 1);
  tmp[1][1][k] = max(tmp[1][1][k], dyn[1][1][last] + 1);
}
void recalc_rev(int k, int last) {
  tmp[0][0][k] = min(tmp[0][0][k], dyn[0][1][last]);
  tmp[0][1][k] = min(tmp[0][1][k], dyn[0][0][last]);
  tmp[1][0][k] = max(tmp[1][0][k], dyn[1][1][last]);
  tmp[1][1][k] = max(tmp[1][1][k], dyn[1][0][last]);
}
int main() {
  string str;
  cin >> str;
  int n = str.size();
  for (int q = 0; q < (int)str.size(); ++q) is_move[q] = str[q] == 'F';
  int k;
  cin >> k;
  for (int q = 0; q <= k; ++q)
    for (int i = 0; i < 2; ++i) {
      dyn[0][i][q] = MAX_INT;
      dyn[1][i][q] = MIN_INT;
    }
  dyn[0][1][0] = dyn[1][1][0] = 0;
  for (int q = 0; q < n; ++q) {
    for (int i = 0; i <= k; ++i)
      for (int j = 0; j < 2; ++j) {
        tmp[0][j][i] = MAX_INT;
        tmp[1][j][i] = MIN_INT;
      }
    if (is_move[q])
      recalc_move(0, 0);
    else
      recalc_rev(0, 0);
    for (int i = 1; i <= k; ++i) {
      if (is_move[q]) {
        for (int j = i; j >= 0; j -= 2) recalc_move(i, j);
        for (int j = i; j - 1 >= 0; j -= 2) recalc_rev(i, j - 1);
      } else {
        for (int j = i; j - 1 >= 0; j -= 2) recalc_move(i, j - 1);
        for (int j = i; j >= 0; j -= 2) recalc_rev(i, j);
      }
    }
    memcpy(dyn, tmp, sizeof(tmp));
  }
  int res = 0;
  for (int q = 0; q < 2; ++q)
    for (int i = 0; i < 2; ++i)
      if (abs(dyn[q][i][k]) < 100000) res = max(res, abs(dyn[q][i][k]));
  cout << res << endl;
  return 0;
}
