#include <bits/stdc++.h>
using namespace std;
string s;
long long n, len;
long long dp[205][102][52][2];
long long fun(long long pos, long long step, long long left, long long sgn) {
  if (step == len) {
    if (left & 1) {
      return 0;
    }
    return abs(pos - 100);
  }
  if (dp[pos][step][left][sgn] != -1) {
    return dp[pos][step][left][sgn];
  }
  long long temp = sgn == 0 ? 1 : -1;
  long long ans = 0;
  if (s[step] == 'F') {
    ans = max(ans, fun(pos + temp, step + 1, left, sgn));
  } else {
    ans = max(ans, fun(pos, step + 1, left, sgn ^ 1));
  }
  if (left) {
    if (s[step] == 'T') {
      ans = max(ans, fun(pos + temp, step + 1, left - 1, sgn));
    } else {
      ans = max(ans, fun(pos, step + 1, left - 1, sgn ^ 1));
    }
  }
  return dp[pos][step][left][sgn] = ans;
}
int main() {
  cin >> s;
  cin >> n;
  len = s.length();
  memset(dp, -1, sizeof(dp));
  cout << fun(100, 0, n, 0);
  return 0;
}
