#include <bits/stdc++.h>
using namespace std;
bool debug = false;
int dpmin[105][110][2];
int dpmax[105][110][2];
string s;
int n;
int main(int argc, char* argv[]) {
  cin >> s >> n;
  memset(dpmin, 0x3f, sizeof(dpmin));
  memset(dpmax, 0xc0, sizeof(dpmax));
  dpmin[0][0][0] = dpmax[0][0][0] = 0;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k < 2; k++) {
        int cost = (s[i] == 'T');
        int newd = k;
        int dx = k ? 1 : -1;
        dpmin[i + 1][j + cost][newd] =
            min(dpmin[i + 1][j + cost][newd], dpmin[i][j][k] + dx);
        dpmax[i + 1][j + cost][newd] =
            max(dpmax[i + 1][j + cost][newd], dpmax[i][j][k] + dx);
        cost = (s[i] == 'F');
        newd = (k ^ 1);
        dx = 0;
        dpmin[i + 1][j + cost][newd] =
            min(dpmin[i + 1][j + cost][newd], dpmin[i][j][k] + dx);
        dpmax[i + 1][j + cost][newd] =
            max(dpmax[i + 1][j + cost][newd], dpmax[i][j][k] + dx);
      }
    }
  }
  int ansmin = 1e9, ansmax = -1e9;
  for (int i = n % 2; i <= n; i += 2) {
    ansmin = min(ansmin, dpmin[s.size()][i][0]);
    ansmin = min(ansmin, dpmin[s.size()][i][1]);
    ansmax = max(ansmax, dpmax[s.size()][i][0]);
    ansmax = max(ansmax, dpmax[s.size()][i][1]);
  }
  cout << max(abs(ansmin), abs(ansmax));
  return 0;
}
