#include <bits/stdc++.h>
using namespace std;
void ga(int N, int *A) {
  for (int i(0); i < N; i++) scanf("%d", A + i);
}
char s[101];
int L, K, dp[101][101][2][2][55];
int dyn(int u, int d, int f, int g, int K) {
  if (u == L) return K ? 0 : d;
  if (!d) d = 0, f = 0;
  int &v = dp[u][d][f][g][K];
  if (~v) return v;
  v = 0;
  if ((s[u] == 'T' && g) || (s[u] == 'F' && !g))
    v = dyn(u + 1, d + (f ? -1 : 1), f, 0, K);
  else
    v = dyn(u + 1, d, !f, 0, K);
  if (K) v = max(v, dyn(u, d, f, !g, K - 1));
  return v;
}
int main(void) {
  scanf("%s%d", s, &K), L = strlen(s), (memset(dp, -1, sizeof(dp)));
  printf("%d\n", dyn(0, 0, 0, 0, K));
  return 0;
}
