#include <bits/stdc++.h>
using namespace std;
int N;
string s;
unsigned char tab[2][51][101][256];
bool was[2][51][101][256];
unsigned char f(bool d = true, unsigned char n = N, unsigned char p = 0,
                unsigned char r = 0) {
  if (p == s.length()) return (n == 0 ? abs((char)r) : 0);
  if (was[d][n][p][r]) return (tab[d][n][p][r]);
  unsigned char res = 0;
  for (int i = 0; i <= n; i++)
    if ((i & 1) && s[p] == 'F' || !(i & 1) && s[p] == 'T')
      res = max(res, f(!d, n - i, p + 1, r));
    else
      res = max(res, f(d, n - i, p + 1, r + (d ? 1 : -1)));
  tab[d][n][p][r] = res;
  was[d][n][p][r] = true;
  return (res);
}
int main() {
  cin >> s >> N;
  cout << (int)f();
exit:
  return (0);
}
