#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
int M[102][52][2][202];
int main() {
  string s;
  int n;
  cin >> s >> n;
  memset(M, 0, sizeof(M));
  M[0][n][1][100] = 1;
  for (int p = 0; p < s.size(); ++p) {
    for (int r = 0; r <= n; ++r) {
      for (int d = 0; d < 2; ++d) {
        for (int x = 0; x <= 201; ++x) {
          if (M[p][r][d][x] == 0) continue;
          for (int ch = 0; ch <= r; ++ch) {
            int nd;
            if (s[p] == 'F') {
              if (ch & 1)
                nd = 1 - d;
              else
                nd = d;
            } else {
              if (ch & 1)
                nd = d;
              else
                nd = 1 - d;
            }
            if (nd == d)
              M[p + 1][r - ch][nd][x + ((nd == 1) ? 1 : -1)] = 1;
            else
              M[p + 1][r - ch][nd][x] = 1;
          }
        }
      }
    }
  }
  int maxim = 0;
  for (int i = 0; i < 202; ++i) {
    if (M[s.size()][0][0][i] or M[s.size()][0][1][i])
      maxim = max(maxim, abs(100 - i));
  }
  cout << maxim << endl;
}
