#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 5;
int dp[N][N][2];
int main() {
  string s;
  cin >> s;
  int n;
  cin >> n;
  int sz = s.size();
  for (int i = sz; i > 0; i--) {
    int x;
    if (i == sz) {
      x = 0;
      if (s[i - 1] == 'F') x = 1;
      for (int oper = 0; oper <= n; oper++) {
        int x2 = x;
        if (oper % 2 == 1) x2 = 1 - x2;
        if (x2 == 1)
          dp[i][oper][0] = dp[i][oper][1] = 1;
        else
          dp[i][oper][0] = dp[i][oper][1] = 0;
      }
      continue;
    }
    for (int cnt_all = 0; cnt_all <= n; cnt_all++) {
      int mx = -INT_MAX, mn = INT_MAX;
      for (int oper_cnt = 0; oper_cnt <= cnt_all; oper_cnt++) {
        x = 0;
        if (s[i - 1] == 'F') x = 1;
        if (oper_cnt % 2 != 0) x = 1 - x;
        if (x == 1) {
          mx = max(mx, 1 + dp[i + 1][cnt_all - oper_cnt][1]);
          mn = min(mn, 1 + dp[i + 1][cnt_all - oper_cnt][0]);
          continue;
        }
        mx = max(mx, -dp[i + 1][cnt_all - oper_cnt][0]);
        mn = min(mn, -dp[i + 1][cnt_all - oper_cnt][1]);
      }
      dp[i][cnt_all][1] = mx;
      dp[i][cnt_all][0] = mn;
    }
  }
  cout << max(abs(dp[1][n][0]), abs(dp[1][n][1]));
}
