#include <bits/stdc++.h>
using namespace std;
bool dp[110][110][220][2];
int main(int argc, char const *argv[]) {
  int N, K, i, j, k, l, i2, j2, k2, l2;
  ;
  string s;
  cin >> s >> K;
  N = s.length();
  dp[0][0][110][0] = true;
  for (int i = 0, _n = (N); i < _n; i++)
    for (int j = 0, _n = (i + 1); j < _n; j++)
      for (int k = 0, _n = (220); k < _n; k++)
        for (int l = 0, _n = (2); l < _n; l++)
          if (dp[i][j][k][l]) {
            i2 = i + 1;
            j2 = j + ((s[i] == 'T') ? 1 : 0);
            k2 = k;
            l2 = 1 - l;
            dp[i2][j2][k2][l2] = true;
            j2 = j + ((s[i] == 'F') ? 1 : 0);
            k2 = k + ((l == 0) ? 1 : -1);
            l2 = l;
            dp[i2][j2][k2][l2] = true;
          }
  int ans = 0;
  for (int i = 0, _n = (220); i < _n; i++)
    for (int j = 0, _n = (2); j < _n; j++)
      for (int k = 0, _n = (N + 1); k < _n; k++)
        if (k <= K && k % 2 == K % 2 && dp[N][N - k][i][j])
          ans = max(ans, abs(i - 110));
  cout << ans << '\n';
  return 0;
}
