#include <bits/stdc++.h>
using namespace std;
int d[101][51][201][2] = {0};
bool used[101][51][201][2] = {false};
int n;
int sz;
string commands;
int rec(int cur, int left, int pos, int dir) {
  if (cur == sz) {
    d[cur][left][100 + pos][(dir + 1) % 2] = abs(pos);
    used[cur][left][100 + pos][(dir + 1) % 2] = true;
    return pos;
  }
  if (used[cur][left][100 + pos][(dir + 1) % 2]) {
    return d[cur][left][100 + pos][(dir + 1) % 2];
  }
  used[cur][left][100 + pos][(dir + 1) % 2] = true;
  int res = 0;
  for (int i = 0; i <= left; ++i) {
    if (cur == sz - 1 && left != 0) {
      i = left;
    }
    int next_pos = pos;
    int next_dir = dir;
    if (i % 2 == 0) {
      if (commands[cur] == 'T') {
        next_dir = -dir;
      } else {
        next_pos = pos + dir;
      }
    } else {
      if (commands[cur] == 'T') {
        next_pos = pos + dir;
      } else {
        next_dir = -dir;
      }
    }
    res = max(res, abs(rec(cur + 1, left - i, next_pos, next_dir)));
  }
  d[cur][left][100 + pos][(dir + 1) % 2] = res;
  return res;
}
int main() {
  int res;
  cin >> commands;
  cin >> n;
  sz = commands.size();
  res = rec(0, n, 0, 1);
  cout << res << endl;
  return 0;
}
