#include <bits/stdc++.h>
using namespace std;
int n;
string s;
int mem[100][220][55][2];
bool doit(int k, int p, int ch, int mov) {
  if (k == -1) return p == 0 && ch == 0 && mov == 1;
  if (p < -101 || p > 101) return false;
  int mdp = (mov == 1) ? 1 : 0;
  if (mem[k][p + 110][ch][mdp] != -1) return mem[k][p + 110][ch][mdp];
  bool valid = false;
  if (s[k] == 'F') {
    for (int i = 0; i <= ch && !valid; i += 2)
      valid = valid || doit(k - 1, p - mov, ch - i, mov);
    for (int i = 1; i <= ch && !valid; i += 2)
      valid = valid || doit(k - 1, p, ch - i, -mov);
  } else {
    for (int i = 0; i <= ch && !valid; i += 2)
      valid = valid || doit(k - 1, p, ch - i, -mov);
    for (int i = 1; i <= ch && !valid; i += 2)
      valid = valid || doit(k - 1, p - mov, ch - i, mov);
  }
  return mem[k][p + 110][ch][mdp] = valid;
}
int main() {
  cin >> s;
  n = s.size();
  int camb;
  cin >> camb;
  memset(mem, -1, sizeof(mem));
  for (int i = 101; i >= 0; i--)
    if (doit(n - 1, i, camb, 1) || doit(n - 1, i, camb, -1) ||
        doit(n - 1, -i, camb, 1) || doit(n - 1, -i, camb, -1)) {
      cout << i << endl;
      return 0;
    }
  return 0;
}
