#include <bits/stdc++.h>
using namespace std;
int dmax[105][55][2], dmin[105][55][2];
char str[1000];
int n, m;
void checkmin(int &a, int b) {
  if (a == -1 || a > b) a = b;
}
void checkmax(int &a, int b) {
  if (a == -1 || a < b) a = b;
}
int op[2] = {1, -1};
int DP() {
  dmax[0][0][0] = dmin[0][0][0] = 100;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= m; j++) {
      for (int d = 0; d < 2; d++) {
        if (dmax[i][j][d] == -1) continue;
        if (str[i + 1] == 'T') {
          checkmax(dmax[i + 1][j][d ^ 1], dmax[i][j][d]);
          checkmin(dmin[i + 1][j][d ^ 1], dmin[i][j][d]);
          checkmax(dmax[i + 1][j + 1][d], dmax[i][j][d] + op[d]);
          checkmin(dmin[i + 1][j + 1][d], dmin[i][j][d] + op[d]);
        }
        if (str[i + 1] == 'F') {
          checkmax(dmax[i + 1][j + 1][d ^ 1], dmax[i][j][d]);
          checkmin(dmin[i + 1][j + 1][d ^ 1], dmin[i][j][d]);
          checkmax(dmax[i + 1][j][d], dmax[i][j][d] + op[d]);
          checkmin(dmin[i + 1][j][d], dmin[i][j][d] + op[d]);
        }
      }
    }
  }
  int ans = 0;
  for (int i = 0; i <= m; i++) {
    if ((i & 1) != (m & 1)) continue;
    for (int d = 0; d < 2; d++) {
      if (dmax[n][i][d] == -1 || dmin[n][i][d] == -1) continue;
      int m1 = abs(dmax[n][i][d] - 100);
      int m2 = abs(dmin[n][i][d] - 100);
      ans = max(ans, max(m1, m2));
    }
  }
  return ans;
}
int main() {
  while (~scanf("%s", str + 1)) {
    scanf("%d", &m);
    n = strlen(str + 1);
    memset(dmax, -1, sizeof(dmax));
    memset(dmin, -1, sizeof(dmin));
    printf("%d\n", DP());
  }
  return 0;
}
