#include <bits/stdc++.h>
using namespace std;
string S;
int n, m;
int dmn[105][105][2];
int dmx[105][105][2];
int i, j, s;
int jj, ss;
int dx;
int main() {
  cin >> S >> n;
  m = S.size();
  memset(dmn, 0x3f, sizeof(dmn));
  memset(dmx, 0xc0, sizeof(dmx));
  dmn[0][0][0] = 0;
  dmx[0][0][0] = 0;
  for (i = 0; i < m; i++)
    for (int j = 0; j <= n; j++)
      for (s = 0; s < 2; s++) {
        jj = (S[i] != 'F') ? (j + 1) : j;
        ss = s;
        dx = s ? -1 : 1;
        dmn[i + 1][jj][ss] = min(dmn[i + 1][jj][ss], dmn[i][j][s] + dx);
        dmx[i + 1][jj][ss] = max(dmx[i + 1][jj][ss], dmx[i][j][s] + dx);
        jj = (S[i] != 'T') ? (j + 1) : j;
        ss = s ^ 1;
        dx = 0;
        dmn[i + 1][jj][ss] = min(dmn[i + 1][jj][ss], dmn[i][j][s] + dx);
        dmx[i + 1][jj][ss] = max(dmx[i + 1][jj][ss], dmx[i][j][s] + dx);
      }
  int amn = 2e9, amx = -2e9;
  for (j = n % 2; j <= n; j += 2)
    for (s = 0; s < 2; s++) {
      amn = min(amn, dmn[m][j][s]);
      amx = max(amx, dmx[m][j][s]);
    }
  int ans = max(abs(amn), abs(amx));
  cout << ans;
  return 0;
}
