#include <bits/stdc++.h>
using namespace std;
const int CURRENRT_DIR = 2;
const int DISTANCE_DIR = 2;
const int CHANGE_USED = 60;
const int COMMAND_AT = 101;
const int POSITIVE = 0;
const int NEGATIVE = 1;
const int INF = 1 << 29;
long long dist[COMMAND_AT][CHANGE_USED][CURRENRT_DIR][DISTANCE_DIR];
char command[COMMAND_AT];
int command_size;
int max_change;
void solve() {
  for (int i = 0; i < COMMAND_AT; i++) {
    for (int j = 0; j < CHANGE_USED; j++) {
      for (int k = 0; k < CURRENRT_DIR; k++) {
        dist[i][j][k][0] = -INF;
        dist[i][j][k][1] = -INF;
      }
    }
  }
  dist[0][0][0][POSITIVE] = 0;
  dist[0][0][0][NEGATIVE] = 0;
  for (int i = 0; i < command_size; i++) {
    for (int j = 0; j <= max_change; j++) {
      for (int k = 0; k < CURRENRT_DIR; k++) {
        if (command[i] == 'T') {
          if (k == 0) {
            for (int t = j; t <= max_change; t += 2)
              if (dist[i + 1][t + 1][k][POSITIVE] <
                  dist[i][j][k][POSITIVE] + 1) {
                dist[i + 1][t + 1][k][POSITIVE] = dist[i][j][k][POSITIVE] + 1;
              }
          } else {
            for (int t = j; t <= max_change; t += 2)
              if (dist[i + 1][t + 1][k][POSITIVE] <
                  dist[i][j][k][POSITIVE] - 1) {
                dist[i + 1][t + 1][k][POSITIVE] = dist[i][j][k][POSITIVE] - 1;
              }
          }
          for (int t = j; t <= max_change; t += 2)
            if (dist[i + 1][t][k][POSITIVE] < dist[i][j][k ^ 1][POSITIVE]) {
              dist[i + 1][t][k][POSITIVE] = dist[i][j][k ^ 1][POSITIVE];
            }
        } else {
          for (int t = j; t <= max_change; t += 2)
            if (dist[i + 1][t + 1][k][POSITIVE] < dist[i][j][k ^ 1][POSITIVE]) {
              dist[i + 1][t + 1][k][POSITIVE] = dist[i][j][k ^ 1][POSITIVE];
            }
          if (k == 0) {
            for (int t = j; t <= max_change; t += 2)
              if (dist[i + 1][t][k][POSITIVE] < dist[i][j][k][POSITIVE] + 1) {
                dist[i + 1][t][k][POSITIVE] = dist[i][j][k][POSITIVE] + 1;
              }
          } else {
            for (int t = j; t <= max_change; t += 2)
              if (dist[i + 1][t][k][POSITIVE] < dist[i][j][k][POSITIVE] - 1) {
                dist[i + 1][t][k][POSITIVE] = dist[i][j][k][POSITIVE] - 1;
              }
          }
        }
        if (command[i] == 'T') {
          if (k == 0) {
            for (int t = j; t <= max_change; t += 2)
              if (dist[i + 1][t + 1][k][NEGATIVE] <
                  dist[i][j][k][NEGATIVE] - 1) {
                dist[i + 1][t + 1][k][NEGATIVE] = dist[i][j][k][NEGATIVE] - 1;
              }
          } else {
            for (int t = j; t <= max_change; t += 2)
              if (dist[i + 1][t + 1][k][NEGATIVE] <
                  dist[i][j][k][NEGATIVE] + 1) {
                dist[i + 1][t + 1][k][NEGATIVE] = dist[i][j][k][NEGATIVE] + 1;
              }
          }
          for (int t = j; t <= max_change; t += 2)
            if (dist[i + 1][t][k][NEGATIVE] < dist[i][j][k ^ 1][NEGATIVE]) {
              dist[i + 1][t][k][NEGATIVE] = dist[i][j][k ^ 1][NEGATIVE];
            }
        } else {
          for (int t = j; t <= max_change; t += 2)
            if (dist[i + 1][t + 1][k][NEGATIVE] < dist[i][j][k ^ 1][NEGATIVE]) {
              dist[i + 1][t + 1][k][NEGATIVE] = dist[i][j][k ^ 1][NEGATIVE];
            }
          if (k == 0) {
            for (int t = j; t <= max_change; t += 2)
              if (dist[i + 1][t][k][NEGATIVE] < dist[i][j][k][NEGATIVE] - 1) {
                dist[i + 1][t][k][NEGATIVE] = dist[i][j][k][NEGATIVE] - 1;
              }
          } else {
            for (int t = j; t <= max_change; t += 2)
              if (dist[i + 1][t][k][NEGATIVE] < dist[i][j][k][NEGATIVE] + 1) {
                dist[i + 1][t][k][NEGATIVE] = dist[i][j][k][NEGATIVE] + 1;
              }
          }
        }
      }
    }
  }
  int max = 0;
  for (int k = 0; k < CURRENRT_DIR; k++) {
    if (max < dist[command_size][max_change][k][0])
      max = dist[command_size][max_change][k][0];
    if (max < dist[command_size][max_change][k][1])
      max = dist[command_size][max_change][k][1];
  }
  cout << max;
}
int main() {
  cin >> command;
  command_size = strlen(command);
  cin >> max_change;
  solve();
}
