#include <bits/stdc++.h>
using namespace std;
string str;
int n;
int dp[200][200][300][3];
int cal(int x, int c, int p, int d) {
  if (c > n) return 0;
  if (x == str.size() - 1) {
    if ((n - c) % 2 == 0) {
      if (str[x] == 'F')
        return abs((p + d) - 0);
      else
        return abs(p - 0);
    } else {
      if (str[x] == 'T')
        return abs((p + d) - 0);
      else
        return abs(p - 0);
    }
  }
  if (dp[x][c][150 + p][1 + d] != -1) return dp[x][c][150 + p][1 + d];
  int res = 0;
  if (str[x] == 'F')
    res = max(res, cal(x + 1, c, p + d, d));
  else
    res = max(res, cal(x + 1, c, p, d * -1));
  if (str[x] == 'T')
    res = max(res, cal(x + 1, c + 1, p + d, d));
  else
    res = max(res, cal(x + 1, c + 1, p, d * -1));
  return dp[x][c][150 + p][1 + d] = res;
}
int main() {
  cin >> str >> n;
  for (int i1 = 0; i1 < 200; i1++)
    for (int i2 = 0; i2 < 200; i2++)
      for (int i3 = 0; i3 < 300; i3++)
        for (int i4 = 0; i4 < 3; i4++) dp[i1][i2][i3][i4] = -1;
  cout << cal(0, 0, 0, 1);
  return 0;
}
