#include <bits/stdc++.h>
using namespace std;
string s;
int best, a[111][55][111][3];
void solve(int pos, int change, int x, int dx) {
  if (a[pos][change][x + 100][dx + 1]++) return;
  if (pos == s.size()) {
    if (change == 0) best = max(best, abs(x));
    return;
  }
  for (int i = 0; i < change + 1; i++) {
    int val = (s[pos] == 'F');
    if (i % 2) val ^= 1;
    solve(pos + 1, change - i, x + (val == 1 ? dx : 0),
          dx * (val == 0 ? -1 : 1));
  }
}
int main() {
  char tmp[111];
  int n;
  scanf("%s%d", tmp, &n);
  s = tmp;
  solve(0, n, 0, 1);
  printf("%d\n", best);
}
