#include <bits/stdc++.h>
using namespace std;
string s;
int n;
int inf = 1000000000;
vector<vector<vector<vector<vector<int> > > > > dp(
    105, vector<vector<vector<vector<int> > > >(
             55, vector<vector<vector<int> > >(
                     2, vector<vector<int> >(55, vector<int>(2, inf + 5)))));
int answer(int nb_considered, int nb_changed, int ending_dir,
           int nb_last_changed, int type) {
  if (dp[nb_considered][nb_changed][ending_dir][nb_last_changed][type] !=
      inf + 5) {
    return dp[nb_considered][nb_changed][ending_dir][nb_last_changed][type];
  }
  if (nb_last_changed > nb_changed) {
    return dp[nb_considered][nb_changed][ending_dir][nb_last_changed][type] =
               inf;
  }
  if (nb_last_changed >= 2) {
    return dp[nb_considered][nb_changed][ending_dir][nb_last_changed][type] =
               answer(nb_considered, nb_changed - 2, ending_dir,
                      nb_last_changed - 2, type);
  }
  if (nb_considered == 1) {
    if (nb_last_changed != nb_changed) {
      return dp[nb_considered][nb_changed][ending_dir][nb_last_changed][type] =
                 inf;
    }
    if ((s[0] == 'T' && nb_last_changed == 0) ||
        (s[0] == 'F' && nb_last_changed == 1)) {
      if (ending_dir == 0) {
        return dp[nb_considered][nb_changed][ending_dir][nb_last_changed]
                 [type] = inf;
      } else {
        return dp[nb_considered][nb_changed][ending_dir][nb_last_changed]
                 [type] = 0;
      }
    }
    if (ending_dir == 1) {
      return dp[nb_considered][nb_changed][ending_dir][nb_last_changed][type] =
                 inf;
    } else {
      return dp[nb_considered][nb_changed][ending_dir][nb_last_changed][type] =
                 1;
    }
  }
  if (nb_changed == 0) {
    int current_pos = 0;
    int current_dir = 0;
    for (int i = 0; i < nb_considered; i++) {
      if (s[i] == 'T') {
        current_dir = 1 - current_dir;
      } else {
        if (current_dir == 0) {
          current_pos++;
        } else {
          current_pos--;
        }
      }
    }
    if (current_dir != ending_dir) {
      return dp[nb_considered][nb_changed][ending_dir][nb_last_changed][type] =
                 inf;
    } else {
      return dp[nb_considered][nb_changed][ending_dir][nb_last_changed][type] =
                 current_pos;
    }
  }
  if (nb_last_changed == 0) {
    if (s[nb_considered - 1] == 'F') {
      bool valid_occured = false;
      int maxx = 0;
      for (int before_type = 0; before_type <= 1; before_type++) {
        for (int before_last = 0; before_last <= nb_changed; before_last++) {
          int tmp = answer(nb_considered - 1, nb_changed, ending_dir,
                           before_last, before_type);
          if (tmp != inf) {
            if (ending_dir == 0) {
              tmp++;
            } else {
              tmp--;
            }
            if (!valid_occured) {
              valid_occured = true;
              maxx = tmp;
            } else {
              if (type == 0) {
                maxx = min(maxx, tmp);
              } else {
                maxx = max(maxx, tmp);
              }
            }
          }
        }
      }
      if (!valid_occured) {
        return dp[nb_considered][nb_changed][ending_dir][nb_last_changed]
                 [type] = inf;
      } else {
        return dp[nb_considered][nb_changed][ending_dir][nb_last_changed]
                 [type] = maxx;
      }
    } else {
      bool valid_occured = false;
      int maxx = 0;
      for (int before_type = 0; before_type <= 1; before_type++) {
        for (int before_last = 0; before_last <= nb_changed; before_last++) {
          int tmp = answer(nb_considered - 1, nb_changed, 1 - ending_dir,
                           before_last, before_type);
          if (tmp != inf) {
            if (!valid_occured) {
              valid_occured = true;
              maxx = tmp;
            } else {
              if (type == 0) {
                maxx = min(maxx, tmp);
              } else {
                maxx = max(maxx, tmp);
              }
            }
          }
        }
      }
      if (!valid_occured) {
        return dp[nb_considered][nb_changed][ending_dir][nb_last_changed]
                 [type] = inf;
      } else {
        return dp[nb_considered][nb_changed][ending_dir][nb_last_changed]
                 [type] = maxx;
      }
    }
  }
  if (nb_last_changed == 1) {
    if (s[nb_considered - 1] == 'F') {
      int tmp1 = answer(nb_considered, nb_changed - 1, 1 - ending_dir, 0, 0);
      int tmp2 = answer(nb_considered, nb_changed - 1, 1 - ending_dir, 0, 1);
      if (tmp1 == inf && tmp2 == inf) {
        return dp[nb_considered][nb_changed][ending_dir][nb_last_changed]
                 [type] = inf;
      }
      if (1 - ending_dir == 0) {
        tmp1--;
        tmp2--;
      } else {
        tmp1++;
        tmp2++;
      }
      if (type == 0) {
        return dp[nb_considered][nb_changed][ending_dir][nb_last_changed]
                 [type] = min(tmp1, tmp2);
      } else {
        return dp[nb_considered][nb_changed][ending_dir][nb_last_changed]
                 [type] = max(tmp1, tmp2);
      }
    } else {
      int tmp1 = answer(nb_considered, nb_changed - 1, 1 - ending_dir, 0, 0);
      int tmp2 = answer(nb_considered, nb_changed - 1, 1 - ending_dir, 0, 1);
      if (tmp1 == inf && tmp2 == inf) {
        return dp[nb_considered][nb_changed][ending_dir][nb_last_changed]
                 [type] = inf;
      }
      if (ending_dir == 0) {
        tmp1++;
        tmp2++;
      } else {
        tmp1--;
        tmp2--;
      }
      if (type == 0) {
        return dp[nb_considered][nb_changed][ending_dir][nb_last_changed]
                 [type] = min(tmp1, tmp2);
      } else {
        return dp[nb_considered][nb_changed][ending_dir][nb_last_changed]
                 [type] = max(tmp1, tmp2);
      }
    }
  }
  return dp[nb_considered][nb_changed][ending_dir][nb_last_changed][type] = inf;
}
int main() {
  cin >> s;
  cin >> n;
  int maxx = 0;
  for (int type = 0; type <= 1; type++) {
    for (int ending_dir = 0; ending_dir <= 1; ending_dir++) {
      for (int last_changed = 0; last_changed <= n; last_changed++) {
        int tmp = answer(s.size(), n, ending_dir, last_changed, type);
        if (tmp != inf) {
          maxx = max(maxx, abs(tmp));
        }
      }
    }
  }
  printf("%d\n", maxx);
  return 0;
}
