#include <bits/stdc++.h>
const int N = 101;
char s[N];
int dp[2][N << 1][N][55];
int abs(int a) { return a > 0 ? a : -a; }
int max(int a, int b) { return a > b ? a : b; }
int dfs(int f, int d, int i, int n) {
  if (!s[i] || n < 0) return n ? 0 : abs(d);
  int &ret = dp[f][d + 100][i][n];
  if (ret != -1) return ret;
  int mv = f ? 1 : -1;
  return ret = max(dfs(f, d + mv, i + 1, n - (s[i] != 'F')),
                   dfs(!f, d, i + 1, n - (s[i] != 'T')));
}
int main() {
  int n, ans = 0;
  scanf("%s%d", s, &n);
  memset(dp, -1, sizeof(dp));
  for (; n >= 0; n -= 2) ans = max(ans, dfs(1, 0, 0, n));
  printf("%d\n", ans);
  return 0;
}
