#include <bits/stdc++.h>
using namespace std;
int m, n;
int t[2][110][60][2];
string s;
int main() {
  for (int i = 0; i < 110; i++)
    for (int j = 0; j < 60; j++) {
      t[0][i][j][0] = t[0][i][j][1] = -1231;
      t[1][i][j][0] = t[1][i][j][1] = 1231;
    }
  cin >> s;
  m = ((int)(s).size());
  scanf("%d", &n);
  t[0][0][0][0] = 0;
  t[1][0][0][0] = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j <= n; j++)
      for (int l = 0; l <= 1; l++) {
        for (int e = 0; e <= 1; e++) {
          if (fabs(t[l][i][j][e]) != 1231) {
            for (int k = 0; j + k <= n; k++) {
              if (k % 2 == 0) {
                if (s[i] == 'F') {
                  t[0][i + 1][j + k][e] =
                      ((t[l][i][j][e] + 1 - 2 * e) > (t[0][i + 1][j + k][e])
                           ? (t[l][i][j][e] + 1 - 2 * e)
                           : (t[0][i + 1][j + k][e]));
                  t[1][i + 1][j + k][e] =
                      ((t[l][i][j][e] + 1 - 2 * e) < (t[1][i + 1][j + k][e])
                           ? (t[l][i][j][e] + 1 - 2 * e)
                           : (t[1][i + 1][j + k][e]));
                } else {
                  t[0][i + 1][j + k][(e + 1) % 2] =
                      ((t[l][i][j][e]) > (t[0][i + 1][j + k][(e + 1) % 2])
                           ? (t[l][i][j][e])
                           : (t[0][i + 1][j + k][(e + 1) % 2]));
                  t[1][i + 1][j + k][(e + 1) % 2] =
                      ((t[l][i][j][e]) < (t[1][i + 1][j + k][(e + 1) % 2])
                           ? (t[l][i][j][e])
                           : (t[1][i + 1][j + k][(e + 1) % 2]));
                }
              } else {
                if (s[i] == 'F') {
                  t[0][i + 1][j + k][(e + 1) % 2] =
                      ((t[l][i][j][e]) > (t[0][i + 1][j + k][(e + 1) % 2])
                           ? (t[l][i][j][e])
                           : (t[0][i + 1][j + k][(e + 1) % 2]));
                  t[1][i + 1][j + k][(e + 1) % 2] =
                      ((t[l][i][j][e]) < (t[1][i + 1][j + k][(e + 1) % 2])
                           ? (t[l][i][j][e])
                           : (t[1][i + 1][j + k][(e + 1) % 2]));
                } else {
                  t[0][i + 1][j + k][e] =
                      ((t[l][i][j][e] + 1 - 2 * e) > (t[0][i + 1][j + k][e])
                           ? (t[l][i][j][e] + 1 - 2 * e)
                           : (t[0][i + 1][j + k][e]));
                  t[1][i + 1][j + k][e] =
                      ((t[l][i][j][e] + 1 - 2 * e) < (t[1][i + 1][j + k][e])
                           ? (t[l][i][j][e] + 1 - 2 * e)
                           : (t[1][i + 1][j + k][e]));
                }
              }
            }
          }
        }
      }
  }
  int res = 0;
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 2; j++)
      if (fabs(t[i][m][n][j]) != 1231)
        res = ((res) > (fabs(t[i][m][n][j])) ? (res) : (fabs(t[i][m][n][j])));
  printf("%d\n", res);
  return 0;
}
