#include <bits/stdc++.h>
using namespace std;
const int M = 200;
const int N = 100200;
const int kind = 26;
const int inf = 1 << 29;
const double eps = 1e-6;
const double pi = acos(-1.0);
const double sq2 = sqrt(2.0);
int d[200][60][2][2];
int n;
int bestmax(int a, int b) {
  if (a == 1000) return b;
  if (b == 1000) return a;
  return max(a, b);
}
int bestmin(int a, int b) {
  if (a == 1000) return b;
  if (b == 1000) return a;
  return min(a, b);
}
int main() {
  string s;
  cin >> s;
  scanf("%d", &n);
  for (int i = 0; i < 200; i++) {
    for (int j = 0; j < 60; j++) {
      for (int k = 0; k < 2; k++) {
        d[i][j][k][0] = d[i][j][k][1] = 1000;
      }
    }
  }
  d[0][n][0][0] = d[0][n][0][1] = 0;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k < 2; k++) {
        for (int t = 0; t < 2; t++) {
          if (d[i][j][k][t] == 1000) continue;
          int step = k == 0 ? 1 : -1;
          if (s[i] == 'T') {
            for (int z = 0; z <= j; z += 2) {
              d[i + 1][j - z][(k + 1) % 2][0] =
                  bestmin(d[i + 1][j - z][(k + 1) % 2][0], d[i][j][k][t]);
              d[i + 1][j - z][(k + 1) % 2][1] =
                  bestmax(d[i + 1][j - z][(k + 1) % 2][1], d[i][j][k][t]);
            }
            for (int z = 1; z <= j; z += 2) {
              d[i + 1][j - z][k][0] =
                  bestmin(d[i + 1][j - z][k][0], d[i][j][k][t] + step);
              d[i + 1][j - z][k][1] =
                  bestmax(d[i + 1][j - z][k][1], d[i][j][k][t] + step);
            }
          } else {
            for (int z = 1; z <= j; z += 2) {
              d[i + 1][j - z][(k + 1) % 2][0] =
                  bestmin(d[i + 1][j - z][(k + 1) % 2][0], d[i][j][k][t]);
              d[i + 1][j - z][(k + 1) % 2][1] =
                  bestmax(d[i + 1][j - z][(k + 1) % 2][1], d[i][j][k][t]);
            }
            for (int z = 0; z <= j; z += 2) {
              d[i + 1][j - z][k][0] =
                  bestmin(d[i + 1][j - z][k][0], d[i][j][k][t] + step);
              d[i + 1][j - z][k][1] =
                  bestmax(d[i + 1][j - z][k][1], d[i][j][k][t] + step);
            }
          }
        }
      }
    }
  }
  int res = 0;
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      if (d[s.size()][0][i][j] != 1000)
        res = max(res, abs(d[s.size()][0][i][j]));
    }
  }
  printf("%d\n", res);
  return 0;
}
