#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
struct node {
  int big, small;
  node() { big = -inf, small = inf; }
} ans[100][55][2], block;
bool vis[100][55][2];
char s[100];
node dfs(int len, int ch, bool dir) {
  if (len < 0) return block;
  if (vis[len][ch][dir]) return ans[len][ch][dir];
  vis[len][ch][dir] = true;
  int p = dir ? 1 : -1;
  node f1, f2;
  if (s[len] == 'F') {
    if (len || ch % 2 == 0)
      f1 = dfs(len - 1, ch, dir), f1.big += p, f1.small += p;
    if ((len || (!len && ch % 2)) && ch) f2 = dfs(len - 1, ch - 1, !dir);
  } else {
    if (len || ch % 2 == 0) f1 = dfs(len - 1, ch, !dir);
    if ((len || (!len && ch % 2)) && ch)
      f2 = dfs(len - 1, ch - 1, dir), f2.big += p, f2.small += p;
  }
  ans[len][ch][dir].big = max(f1.big, f2.big);
  ans[len][ch][dir].small = min(f1.small, f2.small);
  return ans[len][ch][dir];
}
int main() {
  int n;
  block.big = block.small = 0;
  gets(s);
  cin >> n;
  node an = dfs(strlen(s) - 1, n, 1);
  cout << (max(abs(an.big), abs(an.small)));
  return 0;
}
