#include <bits/stdc++.h>
using namespace std;
int ans;
char str[111];
int L;
bool vis[101][51][210][2];
void dfs(int idx, int nLeft, int p, int dir) {
  if (vis[idx][nLeft][p + 100][dir]) return;
  vis[idx][nLeft][p + 100][dir] = 1;
  if (idx == L) {
    if (nLeft == 0) ans = max(ans, abs(p));
    return;
  }
  for (int d = 0; d <= nLeft; d++) {
    char c;
    if (d % 2 == 0)
      c = str[idx];
    else
      c = (str[idx] == 'T' ? 'F' : 'T');
    if (c == 'T')
      dfs(idx + 1, nLeft - d, p, dir ^ 1);
    else
      dfs(idx + 1, nLeft - d, p + (dir ? -1 : 1), dir);
  }
}
int main() {
  scanf("%s", str);
  int N;
  scanf("%d", &N);
  L = strlen(str);
  dfs(0, N, 0, 0);
  cout << ans << endl;
  return 0;
}
