#include <bits/stdc++.h>
using namespace std;
int n;
string s;
int dp[111][55][222][3];
int res;
void dfs(int pos, int left, int x, int dx) {
  if (dp[pos][left][x + 100][dx + 1]) return;
  dp[pos][left][x + 100][dx + 1] = 1;
  if (pos == s.size()) {
    if (left == 0) res = max(res, abs(x));
    return;
  }
  for (int i = 0; i <= left; i++) {
    int ddx = (s[pos] == 'F');
    if (i % 2 == 1) ddx = !ddx;
    dfs(pos + 1, left - i, x + (ddx == 1 ? dx : 0), dx * (ddx == 0 ? -1 : 1));
  }
}
int main() {
  cin >> s;
  cin >> n;
  dfs(0, n, 0, 1);
  cout << res << endl;
  return 0;
}
