#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
char sz[110];
int dp[110][60][2];
int n, m;
int dfs(int x, int y, int dir) {
  if (x == n + 1) {
    if (y == m)
      return 0;
    else
      return -inf;
  }
  if (dp[x][y][dir] > -1000) return dp[x][y][dir];
  int f, s1 = -inf;
  if (dir == 0)
    f = -1;
  else
    f = 1;
  for (int i = y; i <= m; i++) {
    if (sz[x] == 'F') {
      if ((i - y) % 2 == 1)
        s1 = max(dfs(x + 1, i, (dir + 1) % 2), s1);
      else
        s1 = max(dfs(x + 1, i, dir) + f, s1);
    } else {
      if ((i - y) % 2 == 1)
        s1 = max(dfs(x + 1, i, dir) + f, s1);
      else
        s1 = max(dfs(x + 1, i, (dir + 1) % 2), s1);
    }
  }
  if (s1 == -inf) s1 = 0;
  dp[x][y][dir] = s1;
  return s1;
}
int main() {
  scanf("%s", sz + 1);
  scanf("%d", &m);
  n = strlen(sz + 1);
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++)
      for (int g = 0; g < 2; g++) dp[i][j][g] = -1000;
  cout << max(dfs(1, 0, 0), dfs(1, 0, 1)) << endl;
  return 0;
}
