#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
const int del = 10453;
int k;
string s;
long long int dp[200][55][2][200];
long long int ans(int i, int num, bool b, int res) {
  if (dp[i][num][b][res] != -1) return dp[i][num][b][res];
  if (num < 0) return -1;
  if (i == s.size()) {
    if ((num % 2) == 0) return abs(res);
    return -1;
  }
  long long int ress = 0;
  if (b == 1) {
    if (s[i] == 'F') {
      ress = max(ress, ans(i + 1, num, b, res + 1));
      ress = max(ress, ans(i + 1, num - 1, 1 - b, res));
    } else {
      ress = max(ress, ans(i + 1, num, 1 - b, res));
      ress = max(ress, ans(i + 1, num - 1, b, res + 1));
    }
  } else {
    if (s[i] == 'F') {
      ress = max(ress, ans(i + 1, num, b, res - 1));
      ress = max(ress, ans(i + 1, num - 1, 1 - b, res));
    } else {
      ress = max(ress, ans(i + 1, num, 1 - b, res));
      ress = max(ress, ans(i + 1, num - 1, b, res - 1));
    }
  }
  dp[i][num][b][res] = ress;
  return ress;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> s >> k;
  memset(dp, -1, sizeof dp);
  cout << max(ans(0, k, 0, 0), ans(0, k, 1, 0));
  return 0;
}
