#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000,100000000")
using namespace std;
const long long inf = 1e18 + 7;
const long long mod = 1e9 + 7;
const double eps = 1e-9;
const double PI = 2 * acos(0.0);
const double E = 2.71828;
long long n;
char s[100005];
long long f[105][55][205][2];
int main(void) {
  gets(s + 1);
  long long n = 0;
  for (n = 1; s[n]; n++)
    ;
  n--;
  long long m;
  cin >> m;
  f[0][0][100][0] = 1;
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j <= m; j++)
      for (long long k = 0; k < 205; k++)
        for (long long p = 0; p <= 1; p++) {
          if (!f[i][j][k][p]) continue;
          if (s[i + 1] == 'F') {
            long long where = !p ? 1 : -1;
            f[i + 1][j][k + where][p] = 1;
            f[i + 1][j + 1][k][p ^ 1] = 1;
          } else {
            long long where = !p ? 1 : -1;
            f[i + 1][j + 1][k + where][p] = 1;
            f[i + 1][j][k][p ^ 1] = 1;
          }
          f[i][j + 2][k][p] = 1;
        }
  long long Max = 0;
  for (long long i = 0; i <= 204; i++)
    for (long long p = 0; p < 2; p++)
      if (f[n][m][i][p]) Max = max(Max, abs(i - 100));
  cout << Max << endl;
  return 0;
}
