#include <bits/stdc++.h>
using namespace std;
string s;
int n;
int best = 0;
int a[111][55][211][3];
void go(int pos, int change, int x, int dx) {
  if (a[pos][change][x + 100][dx + 1]) return;
  a[pos][change][x + 100][dx + 1] = 1;
  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;
    go(pos + 1, change - i, x + (val == 1 ? dx : 0), dx * (val == 0 ? -1 : 1));
  }
}
int main() {
  cin >> s >> n;
  go(0, n, 0, 1);
  ;
  cout << best;
}
