#include <bits/stdc++.h>
using namespace std;
const int d = 105;
bool f[110][210][60], g[110][210][60];
int n, ans, t;
string st;
int main() {
  getline(cin, st);
  n = st.size();
  cin >> t;
  f[0][d][0] = true;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j < 209; j++) {
      for (int k = 0; k <= t; k++)
        for (int l = 0; l <= k; l++) {
          if (l % 2 == 0) {
            if (st[i - 1] == 'T') {
              if (g[i - 1][j][k - l]) f[i][j][k] = true;
              if (f[i - 1][j][k - l]) g[i][j][k] = true;
            } else {
              if (f[i - 1][j - 1][k - l]) f[i][j][k] = true;
              if (g[i - 1][j + 1][k - l]) g[i][j][k] = true;
            }
          } else {
            if (st[i - 1] == 'T') {
              if (f[i - 1][j - 1][k - l]) f[i][j][k] = true;
              if (g[i - 1][j + 1][k - l]) g[i][j][k] = true;
            } else {
              if (g[i - 1][j][k - l]) f[i][j][k] = true;
              if (f[i - 1][j][k - l]) g[i][j][k] = true;
            }
          }
        }
    }
  ans = 0;
  for (int i = 0; i < 210; i++) {
    if (f[n][i][t]) ans = max(ans, abs(i - d));
    if (g[n][i][t]) ans = max(ans, abs(i - d));
  }
  cout << ans << endl;
  return 0;
}
