#include <bits/stdc++.h>
using namespace std;
bool possible[205][2][51][51];
int main() {
  string s;
  cin >> s;
  int n;
  cin >> n;
  for (int i = 0; i < 205; i++)
    for (int j = 0; j < 2; j++)
      for (int k = 0; k < 51; k++)
        for (int p = 0; p < 51; p++) possible[i][j][k][p] = false;
  possible[101][0][0][0] = true;
  int len = s.size();
  for (int i = 1; i <= len; i++) {
    int ins = 0;
    if (s[i - 1] == 'T') ins = 1;
    for (int j = 101 - len; j <= 101 + len; j++) {
      for (int k = n; k >= 1; k--) {
        if (ins == 0) {
          possible[j][0][i][k] =
              (possible[j - 1][0][i - 1][k] || possible[j][1][i - 1][k - 1]);
          possible[j][1][i][k] =
              (possible[j + 1][1][i - 1][k] || possible[j][0][i - 1][k - 1]);
        } else {
          possible[j][0][i][k] =
              (possible[j - 1][0][i - 1][k - 1] || possible[j][1][i - 1][k]);
          possible[j][1][i][k] =
              (possible[j][0][i - 1][k] || possible[j + 1][1][i - 1][k - 1]);
        }
      }
      if (ins == 0) {
        possible[j][0][i][0] = possible[j - 1][0][i - 1][0];
        possible[j][1][i][0] = possible[j + 1][1][i - 1][0];
      } else {
        possible[j][0][i][0] = possible[j][1][i - 1][0];
        possible[j][1][i][0] = possible[j][0][i - 1][0];
      }
    }
  }
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    if ((n - i) % 2 == 0) {
      for (int j = 101 - len; j <= 101 + len; j++) {
        if (possible[j][0][len][i] || possible[j][1][len][i]) {
          ans = max(ans, abs(j - 101));
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
