#include <bits/stdc++.h>
using namespace std;
string s;
long long n;
long long dp[101][51][2][300];
long long len;
long long f(long long i, long long left, long long ch, long long ans) {
  if (i == len - 1) {
    if (left % 2 == 0) {
      if (s[i] == 'T') return abs(150 - ans);
      long long add = 1;
      if (ch == 1) add = -1;
      return abs(150 - ans - add);
    } else {
      long long add = -1;
      if (ch == 0) add = 1;
      if (s[i] == 'T') return abs(150 - ans - add);
      return abs(150 - ans);
    }
  }
  if (dp[i][left][ch][ans] != -1) return dp[i][left][ch][ans];
  if (left == 0) {
    if (s[i] == 'T') {
      dp[i][left][ch][ans] = f(i + 1, 0, (ch + 1) % 2, ans);
      return dp[i][left][ch][ans];
    } else {
      long long add = -1;
      if (ch == 0) add = 1;
      dp[i][left][ch][ans] = f(i + 1, 0, ch, ans + add);
      return dp[i][left][ch][ans];
    }
  }
  if (s[i] == 'T') {
    long long add = 1;
    if (ch == 1) add = -1;
    dp[i][left][ch][ans] = max(abs(f(i + 1, left - 1, ch, ans + add)),
                               abs(f(i + 1, left, (ch + 1) % 2, ans)));
    return dp[i][left][ch][ans];
  }
  long long add = 1;
  if (ch == 1) add = -1;
  dp[i][left][ch][ans] = max(abs(f(i + 1, left - 1, (ch + 1) % 2, ans)),
                             abs(f(i + 1, left, ch, ans + add)));
  return dp[i][left][ch][ans];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> s;
  len = s.length();
  cin >> n;
  memset(dp, -1, sizeof(dp));
  cout << max(f(0, n, 0, 150), f(0, n, 0, 150));
}
