#include <bits/stdc++.h>
using namespace std;
bool debug = false;
string s;
int n;
int mx = 0;
string path;
bool dp[105][205][3][55];
void dfs(int now, int dis, bool dir, int op) {
  if (dp[now][dis + 101][dir][op]) {
    return;
  }
  dp[now][dis + 101][dir][op] = true;
  if (now == s.size()) {
    if (op == 0) {
      mx = max(mx, abs(dis));
    }
    return;
  }
  for (int i = 0; i <= op; i++) {
    int nowOp = ((s[now] == 'F' ? 0 : 1) + i) % 2;
    if (nowOp == 0) {
      path += "F";
      dfs(now + 1, dis + (dir ? 1 : -1), dir, op - i);
    } else {
      path += "T";
      dfs(now + 1, dis, !dir, op - i);
    }
    path.pop_back();
  }
}
int main(int argc, char* argv[]) {
  cin >> s >> n;
  dfs(0, 0, 1, n);
  cout << mx << endl;
  return 0;
}
