#include <bits/stdc++.h>
using namespace std;
int dp[102][202][51][2], n, K;
string s;
int Magic(int idx, int p, int r, bool dir) {
  if (idx == s.size()) return ((r % 2 == 0) ? p : 0);
  if (dp[idx][p][r][dir] != -1) return dp[idx][p][r][dir];
  int ret = Magic(idx + 1, p + ((s[idx] == 'F') * ((dir == 0) ? 1 : -1)), r,
                  ((s[idx] == 'T') ? (1 - dir) : dir));
  if (r)
    ret = max(ret, Magic(idx + 1, p + ((s[idx] == 'T') * ((dir == 0) ? 1 : -1)),
                         r - 1, ((s[idx] == 'F') ? (1 - dir) : dir)));
  return dp[idx][p][r][dir] = ret;
}
int main() {
  cin >> s >> K;
  memset(dp, -1, sizeof dp);
  cout << max(Magic(0, 100, K, 0), Magic(0, 100, K, 1)) - 100 << endl;
}
