#include <bits/stdc++.h>
using namespace std;
bool can[105][105][220][2];
long mv[10];
string st;
long li;
long n;
long ans;
int main() {
  ios_base::sync_with_stdio(0);
  can[0][0][100][0] = 1;
  cin >> st;
  cin >> li;
  n = st.size();
  mv[0] = 1;
  mv[1] = -1;
  for (int i = 0; i < n; i++)
    for (int ch = 0; ch <= i; ch++)
      for (int ps = 0; ps <= 199; ps++)
        for (int dir = 0; dir <= 1; dir++) {
          if (can[i][ch][ps][dir] == 0) continue;
          if (st[i] == 'F') {
            can[i + 1][ch][ps + mv[dir]][dir] = 1;
            can[i + 1][ch + 1][ps][1 - dir] = 1;
          } else {
            can[i + 1][ch + 1][ps + mv[dir]][dir] = 1;
            can[i + 1][ch][ps][1 - dir] = 1;
          }
        }
  ans = 0;
  long d = 100;
  for (int i = 0; i <= 200; i++)
    for (int j = li; j >= 0; j -= 2)
      for (int dir = 0; dir <= 1; dir++)
        if (can[n][j][i][dir]) {
          ans = max(ans, abs(d - i));
        }
  cout << ans << endl;
  cin.get();
  cin.get();
  return 0;
}
