#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << " " << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
void inp_out() {}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  inp_out();
  string s;
  cin >> s;
  s = ' ' + s;
  long long int n = s.size();
  long long int moves;
  cin >> moves;
  long long int dp[n][moves + 1][2];
  memset(dp, -1, sizeof(dp));
  dp[0][0][0] = dp[0][0][1] = 0;
  for (long long int i = 1; i < n; ++i) {
    for (long long int j = 0; j <= moves; ++j) {
      if (j == 0) {
        if (s[i] == 'T') {
          dp[i][j][0] = dp[i - 1][j][1];
          dp[i][j][1] = dp[i - 1][j][0];
        } else {
          dp[i][j][0] = dp[i - 1][j][0] + 1;
          dp[i][j][1] = dp[i - 1][j][1] - 1;
        }
      } else {
        if (s[i] == 'T') {
          dp[i][j][0] = max(dp[i - 1][j][1], dp[i - 1][j - 1][0] + 1);
          dp[i][j][1] = max(dp[i - 1][j][0], dp[i - 1][j - 1][1] - 1);
        } else {
          dp[i][j][0] = max(dp[i - 1][j][0] + 1, dp[i - 1][j - 1][1]);
          dp[i][j][1] = max(dp[i - 1][j][1] - 1, dp[i - 1][j - 1][0]);
        }
      }
    }
  }
  long long int ans = 0;
  for (long long int i = moves; i >= 0; i -= 2) {
    ans = max({ans, dp[n - 1][i][0], dp[n - 1][i][1]});
  }
  cout << ans;
  return 0;
}
