#include <bits/stdc++.h>
using namespace std;
int d[110][60][2];
const int INF = 1000 * 1000 * 1000;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  string s;
  int n;
  cin >> s >> n;
  for (int i = 0; i < 105; i++)
    for (int j = 0; j < 55; j++) {
      d[i][j][0] = INF;
      d[i][j][1] = -INF;
    }
  d[0][0][0] = 0;
  d[0][0][1] = 0;
  for (int i = 0; i <= s.length(); i++)
    for (int j = 0; j <= n; j++)
      for (int k = 0; k <= 1; k++)
        if (d[i][j][k] != INF && d[i][j][k] != -INF) {
          if (s[i] == 'F' && k == 0) {
            d[i + 1][j][0] = min(d[i + 1][j][0], d[i][j][0] + 1);
          }
          if (s[i] == 'F' && k == 1) {
            d[i + 1][j][1] = max(d[i + 1][j][1], d[i][j][1] + 1);
          }
          if (s[i] == 'T' && k == 0) {
            d[i + 1][j][1] = max(d[i + 1][j][1], -d[i][j][0]);
          }
          if (s[i] == 'T' && k == 1) {
            d[i + 1][j][0] = min(d[i + 1][j][0], -d[i][j][1]);
          }
          if (s[i] == 'T' && k == 0) {
            d[i + 1][j + 1][0] = min(d[i + 1][j + 1][0], d[i][j][0] + 1);
          }
          if (s[i] == 'T' && k == 1) {
            d[i + 1][j + 1][1] = max(d[i + 1][j + 1][1], d[i][j][1] + 1);
          }
          if (s[i] == 'F' && k == 0) {
            d[i + 1][j + 1][1] = max(d[i + 1][j + 1][1], -d[i][j][0]);
          }
          if (s[i] == 'F' && k == 1) {
            d[i + 1][j + 1][0] = min(d[i + 1][j + 1][0], -d[i][j][1]);
          }
        }
  int ans = 0;
  for (int i = 0; i <= n; i++)
    if (i % 2 == n % 2)
      ans = max(ans, max(d[s.length()][i][1], -d[s.length()][i][0]));
  cout << ans;
  return 0;
}
