#include <bits/stdc++.h>
using namespace std;
int n;
char s[105];
bool f[105][55][105 + 105][2];
int main() {
  int m;
  gets(s + 1);
  n = strlen(s + 1);
  scanf("%d", &m);
  for (int i = 1; i <= n; ++i) s[i] = (s[i] == 'F');
  memset(f, 0, sizeof(f));
  f[0][0][n][0] = 1;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j <= m; ++j)
      for (int k = -n; k <= n; ++k)
        for (int p = 0; p < 2; ++p)
          if (f[i][j][k + n][p]) {
            if (s[i + 1] == 1) {
              if (p == 1) {
                f[i + 1][j][k + 1 + n][1] = 1;
                f[i + 1][j + 1][k + n][0] = 1;
              } else {
                f[i + 1][j][k - 1 + n][0] = 1;
                f[i + 1][j + 1][k + n][1] = 1;
              }
            } else {
              if (p == 1) {
                f[i + 1][j][k + n][0] = 1;
                f[i + 1][j + 1][k + 1 + n][1] = 1;
              } else {
                f[i + 1][j][k + n][1] = 1;
                f[i + 1][j + 1][k - 1 + n][0] = 1;
              }
            }
          }
  int ans = 0;
  for (int k = 0; k <= m; ++k)
    if (k % 2 == m % 2)
      for (int i = -n; i <= n; ++i)
        for (int p = 0; p < 2; ++p)
          if (f[n][k][i + n][p]) ans = max(ans, abs(i));
  printf("%d\n", ans);
  return 0;
}
