#include <bits/stdc++.h>
using namespace std;
bool visited[110][110][110 + 110][2][2];
char s[110];
int sLen;
struct State {
  int nChanges, curPos, pos, dir, changeAtCur;
  State(int a, int b, int c, int d, int e)
      : nChanges(a), curPos(b), pos(c), dir(d), changeAtCur(e){};
  State(){};
};
queue<State> q;
int n;
int main() {
  memset(visited, 0, sizeof visited);
  scanf("%s", s);
  scanf("%d", &n);
  sLen = strlen(s);
  q.push(State(0, 0, 0, 0, 0));
  visited[0][0][0][100][0] = true;
  while (!q.empty()) {
    State cur = q.front();
    q.pop();
    int nChanges = cur.nChanges, curPos = cur.curPos, pos = cur.pos,
        dir = cur.dir, changeAtCur = cur.changeAtCur;
    if (curPos == sLen) continue;
    if ((s[curPos] == 'F' && changeAtCur == 0) ||
        (s[curPos] == 'T' && changeAtCur == 1)) {
      if (dir == 0)
        pos++;
      else
        pos--;
      if (!visited[nChanges][curPos + 1][pos + 100][dir][0]) {
        visited[nChanges][curPos + 1][pos + 100][dir][0] = true;
        q.push(State(nChanges, curPos + 1, pos, dir, 0));
      }
      if (dir == 0)
        pos--;
      else
        pos++;
    }
    if ((s[curPos] == 'T' && changeAtCur == 0) ||
        (s[curPos] == 'F' && changeAtCur == 1)) {
      if (dir == 0)
        dir = 1;
      else
        dir = 0;
      if (!visited[nChanges][curPos + 1][pos + 100][dir][0]) {
        visited[nChanges][curPos + 1][pos + 100][dir][0] = true;
        q.push(State(nChanges, curPos + 1, pos, dir, 0));
      }
      if (dir == 0)
        dir = 1;
      else
        dir = 0;
    }
    changeAtCur = (changeAtCur + 1) % 2;
    nChanges++;
    if (nChanges > n) continue;
    if (!visited[nChanges][curPos][pos + 100][dir][changeAtCur]) {
      visited[nChanges][curPos][pos + 100][dir][changeAtCur] = true;
      q.push(State(nChanges, curPos, pos, dir, changeAtCur));
    }
  }
  int res = 0;
  for (int i = -100; i <= 101; i++) {
    if (visited[n][sLen][i + 100][0][0] || visited[n][sLen][i + 100][1][0])
      res = max(res, abs(i));
  }
  cout << res << endl;
  return 0;
}
