#include <bits/stdc++.h>
using namespace std;
int dp[101][51][210][2], n, K;
string s;
int solve(int i, int k, int sum, int d) {
  if (i == n) {
    if (k % 2 == 0) return abs(sum - 100);
    return 0;
  }
  int &ret = dp[i][k][sum][d];
  if (ret + 1) return ret;
  if (k) {
    if (s[i] == 'F')
      ret = max(ret, solve(i + 1, k - 1, sum, !d));
    else {
      if (d)
        ret = max(ret, solve(i + 1, k - 1, sum - 1, d));
      else
        ret = max(ret, solve(i + 1, k - 1, sum + 1, d));
    }
  }
  if (s[i] == 'F') {
    if (d)
      ret = max(ret, solve(i + 1, k, sum - 1, d));
    else
      ret = max(ret, solve(i + 1, k, sum + 1, d));
  } else
    ret = max(ret, solve(i + 1, k, sum, !d));
  return ret;
}
int main() {
  ios::sync_with_stdio(0);
  cin >> s >> K;
  n = s.size();
  memset(dp, -1, sizeof dp);
  cout << solve(0, K, 100, 0);
  return 0;
}
