#include <bits/stdc++.h>
using namespace std;
string str;
int optMinL[110][55];
int optMaxL[110][55];
int n, l;
void calOptL() {
  for (int i = 1; i < l; ++i) {
    for (int j = 0; j <= n; ++j) {
      optMinL[i][j] =
          (str[i] == 'T') ? -optMaxL[i - 1][j] : optMinL[i - 1][j] + 1;
      optMaxL[i][j] =
          (str[i] == 'T') ? -optMinL[i - 1][j] : optMaxL[i - 1][j] + 1;
      for (int k = 0; k <= j; ++k) {
        int add = (str[i] == 'T') ? 1 : 0;
        optMinL[i][j] = min(optMinL[i][j], ((k + add) % 2 == 1)
                                               ? -optMaxL[i - 1][j - k]
                                               : optMinL[i - 1][j - k] + 1);
        optMaxL[i][j] = max(optMaxL[i][j], ((k + add) % 2 == 1)
                                               ? -optMinL[i - 1][j - k]
                                               : optMaxL[i - 1][j - k] + 1);
      }
    }
  }
}
int main() {
  getline(cin, str);
  cin >> n;
  for (int i = 0; i < 110; ++i) {
    for (int j = 0; j < 55; ++j) {
      optMinL[i][j] = -1;
      optMaxL[i][j] = -1;
    }
  }
  l = str.length();
  for (int i = 0; i <= n; ++i) {
    if (i % 2 == 0) {
      optMinL[0][i] = (str[0] == 'F') ? 1 : 0;
      optMaxL[0][i] = (str[0] == 'F') ? 1 : 0;
    } else {
      optMinL[0][i] = (str[0] == 'T') ? 1 : 0;
      optMaxL[0][i] = (str[0] == 'T') ? 1 : 0;
    }
  }
  calOptL();
  printf("%d", max(abs(optMaxL[l - 1][n]), abs(optMinL[l - 1][n])));
}
