#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
const int inf = 1e9;
const long long linf = 1e18;
const long double eps = 1e-6;
const long long mod1 = 1e9 + 7;
using namespace std;
int l[105][51][2], r[105][51][2];
int main() {
  ios_base::sync_with_stdio(0);
  string o;
  cin >> o;
  int n;
  cin >> n;
  for (int i = 0; i < o.size() + 1; ++i)
    for (int j = 0; j < n + 1; ++j)
      for (int k = 0; k < 2; ++k) {
        r[i][j][k] = -inf;
        l[i][j][k] = inf;
      }
  l[0][0][1] = 0;
  r[0][0][1] = 0;
  for (int i = 0; i < o.size(); ++i)
    for (int j = 0; j < n + 1; ++j)
      for (int k = 0; k < 2; ++k) {
        if (l[i][j][k] == inf) continue;
        if (o[i] == 'F') {
          if (k == 1) {
            l[i + 1][j][k] = min(l[i][j][k] + 1, l[i + 1][j][k]);
            r[i + 1][j][k] = max(r[i][j][k] + 1, r[i + 1][j][k]);
          } else {
            l[i + 1][j][k] = min(l[i][j][k] - 1, l[i + 1][j][k]);
            r[i + 1][j][k] = max(r[i][j][k] - 1, r[i + 1][j][k]);
          }
          l[i + 1][j + 1][(k + 1) % 2] =
              min(l[i][j][k], l[i + 1][j + 1][(k + 1) % 2]);
          r[i + 1][j + 1][(k + 1) % 2] =
              max(r[i][j][k], r[i + 1][j + 1][(k + 1) % 2]);
        } else {
          l[i + 1][j][(k + 1) % 2] = min(l[i][j][k], l[i + 1][j][(k + 1) % 2]);
          r[i + 1][j][(k + 1) % 2] = max(r[i][j][k], r[i + 1][j][(k + 1) % 2]);
          if (k == 1) {
            l[i + 1][j + 1][k] = min(l[i][j][k] + 1, l[i + 1][j + 1][k]);
            r[i + 1][j + 1][k] = max(r[i][j][k] + 1, r[i + 1][j + 1][k]);
          } else {
            l[i + 1][j + 1][k] = min(l[i][j][k] - 1, l[i + 1][j + 1][k]);
            r[i + 1][j + 1][k] = max(r[i][j][k] - 1, r[i + 1][j + 1][k]);
          }
        }
      }
  int maxx = -1;
  for (int j = 0; j < n + 1; ++j) {
    for (int k = 0; k < 2; ++k) {
      if (j % 2 == n % 2) {
        if (l[o.size()][j][k] != inf) maxx = max(maxx, abs(l[o.size()][j][k]));
        if (r[o.size()][j][k] != -inf) maxx = max(maxx, abs(r[o.size()][j][k]));
      }
    }
  }
  cout << maxx;
  return 0;
}
