#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10 + 100;
const int INF = int(1e9);
int n, m;
int f[MAXN][MAXN][2];
char s[MAXN];
int main() {
  gets(s);
  m = strlen(s);
  n = ({
    int n;
    scanf(" %d ", &n);
    n;
  });
  memset(f, 0xc4, sizeof(f));
  f[0][0][0] = f[0][0][1] = 0;
  for (int i = (0), _b = (m); i < _b; i++)
    for (int j = (0), _b = (n); j <= _b; j++)
      for (int k = (0), _b = (2); k < _b; k++) {
        if (s[i] == 'T') {
          f[i + 1][j + 1][k] =
              max(f[i + 1][j + 1][k], f[i][j][k] + (k ? 1 : -1));
          f[i + 1][j][!k] = max(f[i + 1][j][!k], f[i][j][k]);
        } else {
          f[i + 1][j + 1][!k] = max(f[i + 1][j + 1][!k], f[i][j][k]);
          f[i + 1][j][k] = max(f[i + 1][j][k], f[i][j][k] + (k ? 1 : -1));
        }
      }
  int res = 0;
  for (int k = n; k >= 0; k -= 2) res = max(res, max(f[m][k][0], f[m][k][1]));
  printf("%d", res);
  return 0;
}
