#include <bits/stdc++.h>
using namespace std;
char s[105];
int n;
int dp[105][55][205][2];
int p[2] = {1, -1};
int main() {
  scanf("%s", s);
  int len = strlen(s);
  int sum = 0;
  for (int i = 0; i < len; i++) {
    if (s[i] == 'T') sum++;
  }
  cin >> n;
  if (n >= sum) {
    n -= sum;
    if (n & 1)
      cout << len - 1 << endl;
    else
      cout << len << endl;
  } else {
    memset(dp, 0, sizeof(dp));
    dp[0][0][100][0] = 1;
    for (int i = 1; i <= len; i++) {
      for (int j = 0; j < i && j <= n; j++) {
        for (int k = 0; k <= 200; k++) {
          for (int t = 0; t < 2; t++) {
            if (!dp[i - 1][j][k][t]) continue;
            if (s[i - 1] == 'T') {
              dp[i][j + 1][k + p[t]][t] = 1;
              dp[i][j][k][t ^ 1] = 1;
            } else {
              dp[i][j][k + p[t]][t] = 1;
              dp[i][j + 1][k][t ^ 1] = 1;
            }
          }
        }
      }
    }
    int ans = 0;
    for (int i = 0; i <= 200; i++) {
      if (dp[len][n][i][1] || dp[len][n][i][0]) {
        ans = max(ans, abs(i - 100));
      }
    }
    cout << ans << endl;
  }
  return 0;
}
