#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 2e9;
const long long INF64 = 9e18;
const long double EPS = 1e-12;
const long double PI =
    3.14159265358979323846264338327950288419716939937510582097;
const long long MD = 1000003;
const long long T = 634521;
const int N = 100010;
const int M = 251;
string s;
int k, n;
bool d[105][205][2][51];
int main() {
  cin >> s >> k;
  n = int((s).size());
  d[0][101][1][0] = 1;
  for (int i = 0; i < int(n); i++)
    for (int j = 0; j <= 202; j++)
      for (int u = 0; u <= k; u++)
        for (int q = 0; q < int(2); q++)
          if (d[i][j][q][u]) {
            int t = q;
            if (s[i] == 'T') {
              int tt = abs(t - 1);
              d[i + 1][j][tt][u] = 1;
            } else {
              if (t == 0)
                d[i + 1][j - 1][t][u] = 1;
              else
                d[i + 1][j + 1][t][u] = 1;
            }
            if (u + 1 <= k)
              if (s[i] == 'F') {
                int tt = abs(t - 1);
                d[i + 1][j][tt][u + 1] = 1;
              } else {
                if (t == 0)
                  d[i + 1][j - 1][t][u + 1] = 1;
                else
                  d[i + 1][j + 1][t][u + 1] = 1;
              }
          }
  int sum = 0;
  for (int j = 0; j < int(202); j++)
    for (int q = 0; q < int(2); q++)
      for (int u = 0; u <= k; u++)
        if (d[n][j][q][u] && (k - u) % 2 == 0) sum = max(sum, abs(101 - j));
  cout << sum;
  return 0;
}
