#include <bits/stdc++.h>
using namespace std;
bool a[101][51][201][3];
int best;
string s;
int n;
void go(int pos, int change, int x, int dx) {
  int lef = dx + 1;
  if (a[pos][change][x + 100][lef]) return;
  a[pos][change][x + 100][lef] = true;
  if (pos == s.size()) {
    if (change == 0) best = max(best, abs(x));
    return;
  }
  for (int i = 0; i <= change; i++) {
    int val = (s[pos] == 'F');
    if (i % 2 == 1) val = !val;
    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 << endl;
  return 0;
}
