#include <bits/stdc++.h>
using namespace std;
int dp[105][105][205][2];
string s;
int mem(int n, int k, int dis, bool dir) {
  if (n == 0) {
    if (dis == 100 && k == 0)
      return true;
    else
      return false;
  }
  int &ret = dp[n][k][dis][dir];
  if (ret != -1) return ret;
  ret = 0;
  if (s[n - 1] == 'T') {
    ret |= mem(n - 1, k, dis, !dir);
    if (dir)
      ret |= mem(n - 1, k - 1, dis - 1, dir);
    else
      ret |= mem(n - 1, k - 1, dis + 1, dir);
  } else {
    ret |= mem(n - 1, k - 1, dis, !dir);
    if (dir)
      ret |= mem(n - 1, k, dis - 1, dir);
    else
      ret |= mem(n - 1, k, dis + 1, dir);
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  memset(dp, -1, sizeof dp);
  dp[0][0][100][0] = dp[0][0][100][1] = 1;
  cin >> s;
  int k;
  cin >> k;
  int ans = 0;
  for (int i = 0; i <= 200; i++)
    for (int j = 0; j <= k; j++) {
      if (mem((int)s.size(), j, i, 0) == 1 && (k - j) % 2 == 0)
        ans = max(ans, abs(100 - i));
      if (mem((int)s.size(), j, i, 1) == 1 && (k - j) % 2 == 0)
        ans = max(ans, abs(100 - i));
    }
  cout << ans << endl;
  return 0;
}
