#include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-9;
int xabs(int a) { return a < 0 ? -a : +a; }
const int maxn = 128;
int dp[maxn][maxn][2];
void upd1(int &x, int y) { x = max(x, y); }
int main() {
  string s;
  cin >> s;
  int n;
  cin >> n;
  for (int i = 0; i < maxn; ++i)
    for (int j = 0; j < maxn; ++j)
      for (int k = 0; k < 2; ++k) dp[i][j][k] = -10000;
  dp[0][0][0] = 0;
  dp[0][0][1] = 0;
  char what[] = {'T', 'F'};
  for (int i = 0; i < s.length(); ++i) {
    for (int j = 0; j <= n; ++j)
      for (int k = 0; k < 2; ++k) {
        for (int w = 0; w <= n - j; ++w) {
          int pos = s[i] == 'T' ? 0 : 1;
          char p = what[(pos + w) % 2];
          if (p == 'T') {
            upd1(dp[i + 1][j + w][1 - k], dp[i][j][k]);
          } else {
            int add = k == 0 ? +1 : -1;
            upd1(dp[i + 1][j + w][k], dp[i][j][k] + add);
          }
        }
      }
  }
  int res = max(dp[s.length()][n][0], dp[s.length()][n][1]);
  cout << res << endl;
  return 0;
}
