#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
int d[maxn][2][maxn];
int n, m;
string ss;
int main() {
  ios::sync_with_stdio(false);
  cin >> ss;
  n = (int)ss.size();
  cin >> m;
  int ma = 0;
  for (int i = 0; i < n; ++i)
    for (int j2 = 0; j2 < 2; ++j2)
      for (int k = 0; k < m + 1; ++k) d[i][j2][k] = -maxn;
  d[0][1][0] = 0;
  for (int i = 0; i < n; ++i)
    for (int j2 = 0; j2 < 2; ++j2)
      for (int k = 0; k < m + 1; ++k) {
        bool bo = ss[i] == 'T';
        if (d[i][j2][k] == -maxn) continue;
        d[i + 1][j2][k + bo] =
            max(d[i + 1][j2][k + bo], d[i][j2][k] + j2 * 2 - 1);
        d[i + 1][!j2][k + !bo] = max(d[i + 1][!j2][k + !bo], d[i][j2][k]);
      }
  for (int i = m; i >= 0; i -= 2) ma = max(max(d[n][0][i], d[n][1][i]), ma);
  for (int i = 0; i < n; ++i)
    for (int j2 = 0; j2 < 2; ++j2)
      for (int k = 0; k < m + 1; ++k) d[i][j2][k] = maxn;
  d[0][1][0] = 0;
  for (int i = 0; i < n; ++i)
    for (int j2 = 0; j2 < 2; ++j2)
      for (int k = 0; k < m + 1; ++k) {
        bool bo = ss[i] == 'T';
        if (d[i][j2][k] == maxn) continue;
        d[i + 1][j2][k + bo] =
            min(d[i + 1][j2][k + bo], d[i][j2][k] + j2 * 2 - 1);
        d[i + 1][!j2][k + !bo] = min(d[i + 1][!j2][k + !bo], d[i][j2][k]);
      }
  for (int i = m; i >= 0; i -= 2) ma = max(-min(d[n][0][i], d[n][1][i]), ma);
  cout << ma << endl;
  return 0;
}
