#include <bits/stdc++.h>
using namespace std;
const int MAXn = 50, MAXl = 100;
bool a[MAXl + 5], mark[MAXl + 2][MAXn + 2][MAXl * 2 + 2][2];
int main() {
  string s;
  int n;
  cin >> s >> n;
  int l = (int)s.size();
  for (int i = 0; i < l; i++)
    if (s[i] == 'T')
      a[i] = true;
    else
      a[i] = false;
  for (int i = 0; i <= n; i++) {
    bool f = a[0];
    if (i % 2) f = !f;
    if (f)
      mark[0][i][100][0] = true;
    else
      mark[0][i][101][1] = true;
  }
  int last = 200;
  for (int i = 1; i < l; i++)
    for (int j = 0; j <= n; j++)
      for (int k = 0; k <= last; k++)
        for (int p = 0; p <= j; p++) {
          bool f = a[i];
          if (p % 2) f = !f;
          if (f) {
            if (mark[i - 1][j - p][k][0]) mark[i][j][k][1] = true;
            if (mark[i - 1][j - p][k][1]) mark[i][j][k][0] = true;
          } else {
            if (k + 1 <= last)
              if (mark[i - 1][j - p][k + 1][0]) mark[i][j][k][0] = true;
            if (k - 1 >= 0)
              if (mark[i - 1][j - p][k - 1][1]) mark[i][j][k][1] = true;
          }
        }
  int Max = 0;
  for (int i = 0; i < 100; i++)
    if (mark[l - 1][n][i][0] || mark[l - 1][n][i][1]) Max = max(Max, 100 - i);
  for (int i = 101; i <= last; i++)
    if (mark[l - 1][n][i][0] || mark[l - 1][n][i][1]) Max = max(Max, i - 100);
  cout << Max << endl;
  return 0;
}
