#include <bits/stdc++.h>
using namespace std;
char s[109];
int n, l, f, t, i;
bool dp[109][350][59][2];
int ans = -(1 << 29);
char opp(char c) {
  if (c == 'F') return 'T';
  return 'F';
}
void go(int l, int pos, int nn, int dir) {
  if (l == strlen(s)) {
    if (nn == n) {
      ans = max(ans, abs(pos));
    }
    return;
  }
  bool &u = dp[l][pos][nn][dir];
  if (u) return;
  u = 1;
  for (int i = 0; i + nn <= n; i++) {
    char c = s[l];
    if (i & 1) c = opp(c);
    if (c == 'T')
      go(l + 1, pos, nn + i, !dir);
    else
      go(l + 1, pos + ((dir == 1) ? 1 : -1), nn + i, dir);
  }
}
int main() {
  gets(s);
  scanf("%d", &n);
  memset(dp, 0, sizeof(dp));
  go(0, 0, 0, 1);
  printf("%d", ans);
}
