#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000000;
const int N = 109;
int f[N][N][2];
int main() {
  std::ios::sync_with_stdio(false);
  string s;
  int k, n;
  int ans = -inf;
  cin >> s >> k;
  n = s.size();
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) f[i][j][0] = f[i][j][1] = -inf;
  f[0][0][1] = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= k; j++) {
      for (int l = 0; l < 2; l++) {
        if (f[i][j][l] == -inf) continue;
        if (i == n) continue;
        if (s[i] == 'F') {
          if (l == 0) {
            f[i + 1][j][0] = max(f[i + 1][j][0], f[i][j][0] - 1);
            f[i + 1][j + 1][1] = max(f[i + 1][j + 1][1], f[i][j][0]);
          } else {
            f[i + 1][j][1] = max(f[i + 1][j][1], f[i][j][1] + 1);
            f[i + 1][j + 1][0] = max(f[i + 1][j + 1][0], f[i][j][1]);
          }
        } else {
          if (l == 0) {
            f[i + 1][j][1] = max(f[i + 1][j][1], f[i][j][0]);
            f[i + 1][j + 1][0] = max(f[i + 1][j + 1][0], f[i][j][0] - 1);
          } else {
            f[i + 1][j][0] = max(f[i + 1][j][0], f[i][j][1]);
            f[i + 1][j + 1][1] = max(f[i + 1][j + 1][1], f[i][j][1] + 1);
          }
        }
      }
    }
  }
  ans = -inf;
  for (int i = 0; i <= k; i++)
    if ((i + k) % 2 == 0) ans = max(ans, max(f[n][i][0], f[n][i][1]));
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) f[i][j][0] = f[i][j][1] = -inf;
  f[0][0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= k; j++) {
      for (int l = 0; l < 2; l++) {
        if (f[i][j][l] == -inf) continue;
        if (s[i] == 'F') {
          if (l == 0) {
            f[i + 1][j][0] = max(f[i + 1][j][0], f[i][j][0] - 1);
            f[i + 1][j + 1][1] = max(f[i + 1][j + 1][1], f[i][j][0]);
          } else {
            f[i + 1][j][1] = max(f[i + 1][j][1], f[i][j][1] + 1);
            f[i + 1][j + 1][0] = max(f[i + 1][j + 1][0], f[i][j][1]);
          }
        } else {
          if (l == 0) {
            f[i + 1][j][1] = max(f[i + 1][j][1], f[i][j][0]);
            f[i + 1][j + 1][0] = max(f[i + 1][j + 1][0], f[i][j][0] - 1);
          } else {
            f[i + 1][j][0] = max(f[i + 1][j][0], f[i][j][1]);
            f[i + 1][j + 1][1] = max(f[i + 1][j + 1][1], f[i][j][1] + 1);
          }
        }
      }
    }
  }
  for (int i = 0; i <= k; i++)
    if ((i + k) % 2 == 0) ans = max(ans, max(f[n][i][0], f[n][i][1]));
  printf("%d\n", ans);
}
