#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
template <typename T>
inline T Abs(T a) {
  return a > 0 ? a : -a;
}
template <typename T>
inline T sqr(T a) {
  return a * a;
}
template <typename T>
inline void relaxMin(T &a, T b) {
  if (b < a) a = b;
}
template <typename T>
inline void relaxMax(T &a, T b) {
  if (b > a) a = b;
}
const int INF = (int)1E9;
const long double EPS = 1E-12;
const long double PI = 3.1415926535897932384626433832795;
int dp[110][60][400][2];
char s[500];
int move1[2] = {1, -1};
void solve() {
  memset(dp, 255, sizeof dp);
  ;
  dp[0][0][200][0] = 0;
  gets(s);
  int n;
  scanf("%d", &n);
  int l = strlen(s);
  for (int i = (0); i < (l); ++i) {
    for (int j = (0); j < (n + 1); ++j) {
      for (int k = (0); k < (400); ++k) {
        for (int u = (0); u < (2); ++u) {
          if (dp[i][j][k][u] != -1) {
            if (s[i] == 'T') {
              for (int v = 0; j + v <= n; v++) {
                if (v & 1)
                  dp[i + 1][v + j][k + move1[u]][u] = 1;
                else
                  dp[i + 1][v + j][k][u ^ 1] = 1;
              }
            }
            if (s[i] == 'F') {
              for (int v = 0; j + v <= n; v++) {
                if (v & 1)
                  dp[i + 1][v + j][k][u ^ 1] = 1;
                else
                  dp[i + 1][v + j][k + move1[u]][u] = 1;
              }
            }
          }
        }
      }
    }
  }
  int dist = -1;
  for (int k = (0); k < (400); ++k)
    for (int u = (0); u < (2); ++u) {
      if (dp[l][n][k][u] != -1) dist = max(dist, abs(k - 200));
    }
  assert(dist != -1);
  cout << dist;
}
int main() {
  solve();
  return 0;
}
