#include <bits/stdc++.h>
using namespace std;
int const N = 101;
int n, m, dp[N][51][N][2];
string s;
int go(int i, int rem, int dis, bool tp) {
  if (i == m) {
    return rem == n ? abs(dis) : -1e9;
  }
  int &ret = dp[i][rem][dis][tp];
  if (~ret) return ret;
  ret = -1e9;
  if (s[i] == 'T') ret = go(i + 1, rem, dis, tp ^ 1);
  if (s[i] == 'F') ret = go(i + 1, rem, dis + (tp ? -1 : 1), tp);
  for (int j = 1; j <= n - rem; ++j) {
    if (j & 1) {
      if (s[i] == 'T')
        ret = max(ret, go(i + 1, rem + j, dis + (tp ? -1 : 1), tp));
      if (s[i] == 'F') ret = max(ret, go(i + 1, rem + j, dis, tp ? 0 : 1));
    } else {
      if (s[i] == 'F')
        ret = max(ret, go(i + 1, rem + j, dis + (tp ? -1 : 1), tp));
      if (s[i] == 'T') ret = max(ret, go(i + 1, rem + j, dis, tp ? 0 : 1));
    }
  }
  return ret;
}
int main() {
  cin >> s >> n;
  m = s.size();
  memset(dp, -1, sizeof dp);
  int an = go(0, 0, 0, 0);
  printf("%d\n", an);
}
