#include <bits/stdc++.h>
using namespace std;
int constexpr INF = numeric_limits<int>::max() / 100;
string cmd;
int change, dp[2][110][60];
bool done[2][110][60];
int f(int d) { return d == 0 ? +1 : -1; }
void upd(int& a, int b) { a = max(a, b); }
int solve(int d, int i, int n) {
  if (i == cmd.size()) {
    if (n > 0)
      return -INF;
    else
      return 0;
  }
  if (done[d][i][n]) return dp[d][i][n];
  int& ans = dp[d][i][n];
  for (int j = 0; j <= n; ++j) {
    if (cmd[i] == 'T') {
      if (j & 1)
        upd(ans, solve(d, i + 1, n - j) + f(d));
      else
        upd(ans, solve(1 - d, i + 1, n - j));
    }
    if (cmd[i] == 'F') {
      if (j & 1)
        upd(ans, solve(1 - d, i + 1, n - j));
      else
        upd(ans, solve(d, i + 1, n - j) + f(d));
    }
  }
  done[d][i][n] = true;
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  fill((int*)dp, (int*)dp + 2 * 110 * 60, -INF);
  cin >> cmd >> change;
  cout << max(solve(0, 0, change), solve(1, 0, change)) << '\n';
}
