#include <bits/stdc++.h>
using namespace std;
int a, b, c, d, x, y, z;
char aa[200];
int dp[205][205][20];
int F(int i, int j, int k) {
  if (i == d && j % 2 == 0) return 0;
  if (i == d && j % 2 != 0) return -999;
  if (dp[i][j][k] != -1) return dp[i][j][k];
  dp[i][j][k] = 0;
  if (aa[i] == 'F' && (!k)) {
    if (j <= 0)
      dp[i][j][k] = F(i + 1, j, k) - 1;
    else
      dp[i][j][k] = max(F(i + 1, j, k) - 1, F(i + 1, j - 1, (k + 1) % 2));
  }
  if (aa[i] == 'F' && (k)) {
    if (j <= 0)
      dp[i][j][k] = F(i + 1, j, k) + 1;
    else
      dp[i][j][k] = max(F(i + 1, j, k) + 1, F(i + 1, j - 1, (k + 1) % 2));
  }
  if (aa[i] == 'T') {
    if (j > 0) {
      if (!k)
        dp[i][j][k] = max(F(i + 1, j, (k + 1) % 2), F(i + 1, j - 1, k) - 1);
      if (k)
        dp[i][j][k] = max(F(i + 1, j, (k + 1) % 2), F(i + 1, j - 1, k) + 1);
    } else
      dp[i][j][k] = F(i + 1, j, (k + 1) % 2);
  }
  return dp[i][j][k];
}
int f(int i, int j, int k) {
  if (i == d && j % 2 == 0) return 0;
  if (i == d && j % 2 != 0) return -999;
  if (dp[i][j][k] != -1) return dp[i][j][k];
  dp[i][j][k] = 0;
  if (aa[i] == 'F' && (!k)) {
    if (j <= 0)
      dp[i][j][k] = f(i + 1, j, k) + 1;
    else
      dp[i][j][k] = max(f(i + 1, j, k) + 1, f(i + 1, j - 1, (k + 1) % 2));
  }
  if (aa[i] == 'F' && (k)) {
    if (j <= 0)
      dp[i][j][k] = f(i + 1, j, k) - 1;
    else
      dp[i][j][k] = max(f(i + 1, j, k) - 1, f(i + 1, j - 1, (k + 1) % 2));
  }
  if (aa[i] == 'T') {
    if (j > 0) {
      if (!k)
        dp[i][j][k] = max(f(i + 1, j, (k + 1) % 2), f(i + 1, j - 1, k) + 1);
      if (k)
        dp[i][j][k] = max(f(i + 1, j, (k + 1) % 2), f(i + 1, j - 1, k) - 1);
    } else
      dp[i][j][k] = f(i + 1, j, (k + 1) % 2);
  }
  return dp[i][j][k];
}
int main() {
  while (cin >> aa) {
    scanf("%d", &a);
    memset(dp, -1, sizeof(dp));
    d = strlen(aa);
    b = f(0, a, 0);
    memset(dp, -1, sizeof(dp));
    b = max(b, F(0, a, 0));
    cout << b << endl;
  }
}
