#include <bits/stdc++.h>
using namespace std;
void __dbg() { cout << endl; }
template <typename Arg, typename... Args>
void __dbg(Arg A, Args... B) {
  cout << " " << A;
  __dbg(B...);
}
const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 1e9 + 7;
const int maxN = 100 + 10;
int dp1[maxN][maxN][2], dp2[maxN][maxN][2];
void umax(int& a, int b) {
  if (b > a) a = b;
}
void umin(int& a, int b) {
  if (b < a) a = b;
}
int main() {
  string s;
  int C;
  cin >> s >> C;
  int n = (int)s.size();
  for (int i = 0; i < n + 1; i++) {
    for (int changes = 0; changes < C + 1; changes++) {
      for (int dir = 0; dir < 2; dir++) {
        dp1[i][changes][dir] = ninf;
        dp2[i][changes][dir] = inf;
      }
    }
  }
  dp1[0][0][0] = dp2[0][0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int changes = 0; changes < C + 1; changes++) {
      for (int dir = 0; dir < 2; dir++) {
        if (dp1[i][changes][dir] != ninf) {
          for (int has = 0; has < C - changes + 1; has++) {
            int new_dir = dir, command = (s[i] == 'T') ? 0 : 1;
            if (has & 1) command ^= 1;
            if (dir) command *= -1;
            if (!command) new_dir ^= 1;
            umax(dp1[i + 1][changes + has][new_dir],
                 dp1[i][changes][dir] + command);
          }
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int changes = 0; changes < C + 1; changes++) {
      for (int dir = 0; dir < 2; dir++) {
        if (dp2[i][changes][dir] != inf) {
          for (int has = 0; has < C - changes + 1; has++) {
            int new_dir = dir, command = (s[i] == 'T') ? 0 : 1;
            if (has & 1) command ^= 1;
            if (dir) command *= -1;
            if (!command) new_dir ^= 1;
            umin(dp2[i + 1][changes + has][new_dir],
                 dp2[i][changes][dir] + command);
          }
        }
      }
    }
  }
  int ans = ninf;
  for (int dir = 0; dir < 2; dir++) {
    ans = max(ans, dp1[n][C][dir]);
    if (dp2[n][C][dir] != inf) ans = max(ans, abs(dp2[n][C][dir]));
  }
  printf("%d\n", ans);
  return 0;
}
