#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int MAX = 1e9 + 5;
int i, j, k, n, m, dp[105][105][5][2];
int dp2[105][105][5][2];
string str;
int f(int x, int k, int c, int l) {
  if (x >= str.size() && k == n) return 0;
  if (k > n) return -99999;
  if (x >= str.size() && k != n) return -99999;
  if (dp[x][k][c][l]) return dp[x][k][c][l];
  if ((str[x] == 'F' && !l) || (l && str[x] == 'T'))
    return dp[x][k][c][l] = max(f(x, k + 1, c, !l), f(x + 1, k, c, 0) + c - 1);
  else
    return dp[x][k][c][l] =
               max(f(x, k + 1, c, !l), f(x + 1, k, ((c == 0) ? 2 : 0), 0));
}
int f2(int x, int k, int c, int l) {
  if (x >= str.size() && k == n) return 0;
  if (x >= str.size() && k != n) return 99999;
  if (k > n) return 9999;
  if (dp2[x][k][c][l]) return dp2[x][k][c][l];
  if ((str[x] == 'F' && !l) || (l && str[x] == 'T'))
    return dp2[x][k][c][l] =
               min(f2(x, k + 1, c, !l), f2(x + 1, k, c, 0) + c - 1);
  else
    return dp2[x][k][c][l] =
               min(f2(x, k + 1, c, !l), f2(x + 1, k, ((c == 0) ? 2 : 0), 0));
}
int main() {
  cin >> str >> n;
  cout << max(abs(f(0, 0, 2, 0)), abs(f2(0, 0, 2, 0)));
  return 0;
}
