#include <bits/stdc++.h>
using namespace std;
long long dp[102][2][502][52];
long long n;
string s;
long long cal(long long pos, long long dis, long long sum, long long res) {
  if (pos >= s.size()) {
    if (res == 0) {
      if (sum < 200)
        return 200 - sum;
      else
        return sum - 200;
    }
    return 0;
  }
  if (dp[pos][dis][sum][res] != -1) return dp[pos][dis][sum][res];
  long long val = 0;
  for (long long j = 0;; j++) {
    if (j > res) break;
    if (j % 2) {
      if (dis) {
        if (s[pos] == 'F')
          val = max(val, cal(pos + 1, dis ^ 1, sum, res - j));
        else
          val = max(val, cal(pos + 1, dis, sum + 1, res - j));
      } else {
        if (s[pos] == 'F')
          val = max(val, cal(pos + 1, dis ^ 1, sum, res - j));
        else
          val = max(val, cal(pos + 1, dis, sum - 1, res - j));
      }
    } else {
      if (dis) {
        if (s[pos] == 'T')
          val = max(val, cal(pos + 1, dis ^ 1, sum, res - j));
        else
          val = max(val, cal(pos + 1, dis, sum + 1, res - j));
      } else {
        if (s[pos] == 'T')
          val = max(val, cal(pos + 1, dis ^ 1, sum, res - j));
        else
          val = max(val, cal(pos + 1, dis, sum - 1, res - j));
      }
    }
  }
  return dp[pos][dis][sum][res] = val;
}
int main() {
  long long i, j, k, l, m;
  cin >> s;
  n = s.size();
  cin >> k;
  memset(dp, -1, sizeof dp);
  cout << cal(0, 1, 200, k) << '\n';
}
