#include <bits/stdc++.h>
using namespace std;
bool dp[110][350][60][2];
string second;
int n, ans = -(1 << 29);
char opp(char ch) {
  if (ch == 'F') return 'T';
  return 'F';
}
void Calc(int cur, int pos, int nn, int dir) {
  if (cur == second.size()) {
    if (n == nn) {
      ans = max(ans, abs(pos - 150));
      return;
    } else
      return;
  }
  bool &ans = dp[cur][pos][nn][dir];
  if (ans) return;
  ans = true;
  for (int i = 0; i + nn <= n; ++i) {
    char ch = second[cur];
    if (i & 1) ch = opp(ch);
    if (ch == 'T')
      Calc(cur + 1, pos, i + nn, !dir);
    else
      Calc(cur + 1, pos + ((dir == 1) ? 1 : -1), i + nn, dir);
  }
}
int main() {
  getline(cin, second);
  cin >> n;
  memset(dp, 0, sizeof(dp));
  Calc(0, 150, 0, 1);
  cout << ans << endl;
  return 0;
}
