#include <bits/stdc++.h>
using namespace std;
const int N = 50 + 5, M = 100 + 5;
char st[M];
bool f[M][N][2][2 * M];
int op[2] = {1, -1};
int main() {
  scanf("%s", st);
  int n, m = strlen(st);
  scanf("%d", &n);
  memset(f, 0, sizeof(f));
  f[0][0][0][100] = 1;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k < 2; k++) {
        for (int l = 100 - m; l <= 100 + m; l++)
          if (f[i][j][k][l]) {
            for (int now = j; now <= n; now += 2) {
              if (st[i] == 'T') {
                f[i + 1][now][k ^ 1][l] = 1;
              } else {
                f[i + 1][now][k][l + op[k]] = 1;
              }
            }
            for (int now = j + 1; now <= n; now += 2) {
              if (st[i] == 'T') {
                f[i + 1][now][k][l + op[k]] = 1;
              } else {
                f[i + 1][now][k ^ 1][l] = 1;
              }
            }
          }
      }
    }
  }
  int ans = -1;
  for (int k = 0; k < 2; k++) {
    for (int l = 100 - m; l <= 100 + m; l++)
      if (f[m][n][k][l]) {
        ans = max(ans, abs(100 - l));
      }
  }
  printf("%d\n", ans);
  return 0;
}
