#include <bits/stdc++.h>
using namespace std;
string s;
int len, t, mx;
int dp[110][60][210][5];
void dfs(int pos, int ch, int x, int dx) {
  if (dp[pos][ch][x + 100][dx + 1]) return;
  dp[pos][ch][x + 100][dx + 1] = 1;
  if (pos == len) {
    if (ch == 0) mx = max(mx, abs(x));
    return;
  }
  for (int i = 0; i <= ch; i++) {
    int val = (s[pos] == 'F');
    if (i % 2) val ^= 1;
    dfs(pos + 1, ch - i, x + (val == 1 ? dx : 0), dx * (val == 0 ? -1 : 1));
  }
}
int main() {
  cin >> s >> t;
  len = s.size();
  dfs(0, t, 0, 1);
  cout << mx << endl;
  return 0;
}
