#include <bits/stdc++.h>
using namespace std;
char work[105];
int len, n, dp[2][105 << 1][105][52];
int dfs(bool dre, int pos, int i, int cn) {
  if (cn < 0) return 0;
  if (!work[i]) return cn > 0 ? 0 : abs(pos);
  int &ret = dp[dre][pos + 100][i][cn];
  if (ret != -1) return ret;
  int mv = dre ? 1 : -1;
  return ret = max(dfs(dre, pos + mv, i + 1, cn - (work[i] != 'F')),
                   dfs(!dre, pos, i + 1, cn - (work[i] != 'T')));
}
int main(void) {
  int ans = 0;
  scanf("%s%d", work, &n);
  memset(dp, -1, sizeof(dp));
  for (; n >= 0; n -= 2) ans = max(ans, dfs(1, 0, 0, n));
  printf("%d\n", ans);
  return 0;
}
