#include <bits/stdc++.h>
using namespace std;
const int oo = 1000000000;
char a[110];
int n;
bool dp[110][210][60][2];
int main() {
  while (~scanf("%s", a)) {
    scanf("%d", &n);
    memset(dp, 0, sizeof(dp));
    int l = strlen(a);
    dp[0][101][n][0] = 1;
    for (int i = 1; i <= l; i++)
      for (int pos = 1; pos <= 201; pos++)
        for (int k = n; k >= 0; k--) {
          if (a[i - 1] == 'F') {
            if (dp[i - 1][pos - 1][k][0]) dp[i][pos][k][0] = 1;
            if (dp[i - 1][pos][k + 1][1]) dp[i][pos][k][0] = 1;
            if (dp[i - 1][pos + 1][k][1]) dp[i][pos][k][1] = 1;
            if (dp[i - 1][pos][k + 1][0]) dp[i][pos][k][1] = 1;
          } else {
            if (dp[i - 1][pos - 1][k + 1][0]) dp[i][pos][k][0] = 1;
            if (dp[i - 1][pos][k][1]) dp[i][pos][k][0] = 1;
            if (dp[i - 1][pos + 1][k + 1][1]) dp[i][pos][k][1] = 1;
            if (dp[i - 1][pos][k][0]) dp[i][pos][k][1] = 1;
          }
        }
    int ans = 0;
    for (int i = 0; i <= n; i += 2)
      for (int j = 1; j <= 201; j++)
        for (int k = 0; k < 2; k++)
          if (dp[l][j][i][k]) ans = max(ans, abs(j - 101));
    printf("%d\n", ans);
  }
}
