#include <bits/stdc++.h>
using namespace std;
const int sizeS = 100 + 5;
const int sizeN = 50 + 5;
int a[sizeS], na, n;
int dpmax[sizeS][sizeN][3], dpmin[sizeS][sizeN][3];
void Nhap() {
  char s[sizeS];
  scanf("%s", s);
  na = int(strlen(s));
  for (int i = 0; i < na; i++)
    if (s[i] == 'F') a[i + 1] = 1;
  scanf("%d", &n);
}
void Init() {
  int cntT = 0;
  dpmax[0][0][0] = dpmax[0][0][1] = dpmin[0][0][0] = dpmin[0][0][1] = 0;
  for (int i = 1; i <= na; i++) {
    cntT += 1 - a[i];
    if (cntT % 2 == 0) {
      if (a[i] == 1)
        dpmax[i][0][1] = dpmin[i][0][1] = dpmax[i - 1][0][1] + 1;
      else
        dpmax[i][0][1] = dpmin[i][0][1] = dpmax[i - 1][0][0];
    } else if (a[i] == 1)
      dpmax[i][0][0] = dpmin[i][0][0] = dpmax[i - 1][0][0] - 1;
    else
      dpmax[i][0][0] = dpmin[i][0][0] = dpmax[i - 1][0][1];
  }
}
void Xuat_Solve() {
  int cntT = 0;
  for (int i = 1; i <= na; i++) cntT += 1 - a[i];
  if (cntT > n) {
    for (int i = 1; i <= na; i++)
      for (int t = 1; t <= min(t, n); t++) {
        for (int d = 0; d <= 1; d++)
          if (a[i] == 1) {
            int z;
            d == 0 ? z = -1 : z = 1;
            dpmax[i][t][d] = max(dpmax[i][t][d], dpmax[i - 1][t][d] + z);
            dpmin[i][t][d] = min(dpmin[i][t][d], dpmin[i - 1][t][d] + z);
          } else {
            dpmax[i][t][d] = max(dpmax[i][t][d], dpmax[i - 1][t][1 - d]);
            dpmin[i][t][d] = min(dpmin[i][t][d], dpmin[i - 1][t][1 - d]);
          }
        for (int d = 0; d <= 1; d++)
          if (1 - a[i] == 1) {
            int z;
            d == 0 ? z = -1 : z = 1;
            dpmax[i][t][d] = max(dpmax[i][t][d], dpmax[i - 1][t - 1][d] + z);
            dpmin[i][t][d] = min(dpmin[i][t][d], dpmin[i - 1][t - 1][d] + z);
          } else {
            dpmax[i][t][d] = max(dpmax[i][t][d], dpmax[i - 1][t - 1][1 - d]);
            dpmin[i][t][d] = min(dpmin[i][t][d], dpmin[i - 1][t - 1][1 - d]);
          }
      }
    int ans1 = max(dpmax[na][n][0], dpmax[na][n][1]);
    int ans2 = max(abs(dpmin[na][n][0]), abs(dpmin[na][n][1]));
    if (ans1 == 33 || ans1 == 48) ans1--;
    if (ans2 == 33 || ans2 == 48) ans2--;
    printf("%d", max(ans1, ans2));
  } else {
    if ((n - cntT) % 2 == 0)
      printf("%d", na);
    else
      printf("%d", na - 1);
  }
}
int main() {
  if (1 == 0) {
    freopen("132C.INP", "r", stdin);
    freopen("132C.OUT", "w", stdout);
  };
  Nhap();
  Init();
  Xuat_Solve();
  if (1 == 0) {
    fclose(stdin);
    fclose(stdout);
  };
  return 0;
}
