#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/stack:200000000")
int dp[110][210][55][3], arr[110], n, len;
void solve(int pos, int x, int m, int dir) {
  if (pos == len && m == n) {
    dp[pos][x][m][dir + 1] = 1;
    return;
  }
  if (pos >= len || m > n) return;
  if (dp[pos][x][m][dir + 1] != -1) return;
  if (arr[pos] == 0)
    solve(pos + 1, x, m, -dir);
  else
    solve(pos + 1, x + dir, m, dir);
  if (m < n) {
    if (arr[pos] == 0)
      solve(pos + 1, x + dir, m + 1, dir);
    else
      solve(pos + 1, x, m + 1, -dir);
  }
  if (m < n - 1) solve(pos, x, m + 2, dir);
  dp[pos][x][m][dir + 1] = 1;
}
int main() {
  fill(&dp[0][0][0][0], &dp[0][0][0][0] + 210 * 110 * 55 * 3, -1);
  string str;
  cin >> str;
  len = str.size();
  for (int i = 0; i < len; i++)
    if (str[i] == 'T')
      arr[i] = 0;
    else
      arr[i] = 1;
  cin >> n;
  solve(0, 101, 0, 1);
  int best = -1;
  for (int i = 0; i < 210; i++) {
    if (dp[len][i][n][0] != -1) best = max(best, abs(101 - i));
    if (dp[len][i][n][2] != -1) best = max(best, abs(101 - i));
    if (best == 108) int y = 5;
  }
  cout << best << endl;
  return 0;
}
