#include <bits/stdc++.h>
using namespace std;
int d[2] = {1, -1};
int tp, k2, i, j, k, num;
char s[105];
void dp(int f[][55][2], int type) {
  tp = f[i][j][k], k2 = k;
  if (num & 1) {
    if (s[i] == 'T')
      tp += d[k];
    else
      k2 = !k;
  } else {
    if (s[i] == 'T')
      k2 = !k;
    else
      tp += d[k];
  }
  if (type == 1)
    f[i + 1][j + num][k2] = max(f[i + 1][j + num][k2], tp);
  else
    f[i + 1][j + num][k2] = min(f[i + 1][j + num][k2], tp);
}
int f[105][55][2], g[105][55][2];
int main() {
  int n, len;
  while (cin >> s) {
    len = strlen(s);
    cin >> n;
    for (i = 0; i < 105; i++) {
      for (j = 0; j < 55; j++) {
        f[i][j][0] = f[i][j][1] = -0x3fffffff;
        g[i][j][0] = g[i][j][1] = 0x3fffffff;
      }
    }
    f[0][0][0] = f[0][0][1] = g[0][0][0] = g[0][0][1] = 0;
    for (i = 0; i < len; i++) {
      for (j = 0; j <= n; j++) {
        for (k = 0; k < 2; k++) {
          for (num = 0; j + num <= n; num++) {
            if (f[i][j][k] > -0x3fffffff) dp(f, 1);
            if (g[i][j][k] < 0x3fffffff) dp(g, 0);
          }
        }
      }
    }
    cout << max(f[len][n][0],
                max(f[len][n][1], max(-g[len][n][0], -g[len][n][1])))
         << endl;
  }
  return 0;
}
