#include <bits/stdc++.h>
using namespace std;
int l[105][55][5], r[105][55][5], napr = 1;
int main() {
  string s;
  int n;
  cin >> s >> n;
  for (int i = 0; i <= s.size(); ++i)
    for (int j = 0; j <= n; ++j) {
      l[i][j][1] = l[i][j][2] = 1e9;
      r[i][j][1] = r[i][j][2] = -1e9;
    }
  l[0][0][2] = 0;
  r[0][0][2] = 0;
  for (int i = 0; i < s.size(); ++i) {
    for (int j = 0; j <= n; ++j)
      if (s[i] == 'F') {
        for (int d = 0; j + 1 + d <= n; d += 2) {
          l[i + 1][j + 1 + d][1] = min(l[i + 1][j + 1 + d][1], l[i][j][2]);
          l[i + 1][j + 1 + d][2] = min(l[i + 1][j + 1 + d][2], l[i][j][1]);
          r[i + 1][j + 1 + d][1] = max(r[i + 1][j + 1 + d][1], r[i][j][2]);
          r[i + 1][j + 1 + d][2] = max(r[i + 1][j + 1 + d][2], r[i][j][1]);
        }
        for (int d = 0; j + d <= n; d += 2) {
          l[i + 1][j + d][1] = min(l[i + 1][j + d][1], l[i][j][1] - 1);
          l[i + 1][j + d][2] = min(l[i + 1][j + d][2], l[i][j][2] + 1);
          r[i + 1][j + d][1] = max(r[i + 1][j + d][1], r[i][j][1] - 1);
          r[i + 1][j + d][2] = max(r[i + 1][j + d][2], r[i][j][2] + 1);
        }
      } else {
        for (int d = 0; j + 1 + d <= n; d += 2) {
          l[i + 1][j + 1 + d][1] = min(l[i + 1][j + 1 + d][1], l[i][j][1] - 1);
          l[i + 1][j + 1 + d][2] = min(l[i + 1][j + 1 + d][2], l[i][j][2] + 1);
          r[i + 1][j + 1 + d][1] = max(r[i + 1][j + 1 + d][1], r[i][j][1] - 1);
          r[i + 1][j + 1 + d][2] = max(r[i + 1][j + 1 + d][2], r[i][j][2] + 1);
        }
        for (int d = 0; j + d <= n; d += 2) {
          l[i + 1][j + d][1] = min(l[i + 1][j + d][1], l[i][j][2]);
          l[i + 1][j + d][2] = min(l[i + 1][j + d][2], l[i][j][1]);
          r[i + 1][j + d][1] = max(r[i + 1][j + d][1], r[i][j][2]);
          r[i + 1][j + d][2] = max(r[i + 1][j + d][2], r[i][j][1]);
        }
      }
  }
  int result = 0;
  for (int i = n; i >= 0; i -= 2) {
    int minl = min(l[s.size()][i][1], l[s.size()][i][2]);
    int maxr = max(r[s.size()][i][1], r[s.size()][i][2]);
    int MAX = max(-minl, maxr);
    result = max(result, MAX);
  }
  cout << result << endl;
  return 0;
}
