#include <bits/stdc++.h>
using namespace std;
int pd[210][101][51][2];
char in[102];
int topdown(int pos, int f, int c, int d) {
  if (in[f] != 'F' && in[f] != 'T') {
    if (c == 0)
      return abs(100 - pos);
    else
      return 0;
  }
  int add = d ? 1 : -1;
  if (pd[pos][f][c][d] != -1) return pd[pos][f][c][d];
  if (c == 0) {
    if (in[f] == 'F')
      return topdown(pos + add, f + 1, c, d);
    else
      return topdown(pos, f + 1, c, (d + 1) % 2);
  }
  int best = -1;
  if (in[f] == 'F')
    best = max(best, topdown(pos + add, f + 1, c, d));
  else
    best = max(best, topdown(pos, f + 1, c, (d + 1) % 2));
  if (in[f] == 'F')
    best = max(best, topdown(pos, f + 1, c - 1, (d + 1) % 2));
  else
    best = max(best, topdown(pos + add, f + 1, c - 1, d));
  return pd[pos][f][c][d] = best;
}
int main() {
  gets(in);
  int n;
  scanf(" %d", &n);
  memset(pd, -1, sizeof pd);
  int res = 0;
  for (int i = 0; i <= n; i += 2) res = max(res, topdown(100, 0, n - i, 0));
  printf("%d\n", res);
  return 0;
}
