#include <bits/stdc++.h>
using namespace std;
int dp[2][205][105][55], len;
char s[105];
int dfs(int go, int pos, int i, int cnt) {
  if (cnt < 0) return 0;
  if (i >= len) return cnt > 0 ? 0 : abs(pos);
  if (dp[go][pos + 100][i][cnt] != -1) return dp[go][pos + 100][i][cnt];
  dp[go][pos + 100][i][cnt] =
      max(dfs(!go, pos, i + 1, cnt - (s[i] != 'T')),
          dfs(go, pos + (go ? 1 : -1), i + 1, cnt - (s[i] != 'F')));
  return dp[go][pos + 100][i][cnt];
}
int main() {
  int ans, cnt;
  scanf("%s%d", s, &cnt);
  memset(dp, -1, sizeof(dp));
  ans = 0;
  len = strlen(s);
  while (cnt >= 0) {
    ans = max(ans, dfs(1, 0, 0, cnt));
    cnt -= 2;
  }
  printf("%d\n", ans);
  return 0;
}
