#include <bits/stdc++.h>
using namespace std;
int read() {
  int n;
  char c;
  bool neg;
  n = 0;
  neg = false;
  for (c = getchar(); !(c >= '0' && c <= '9') && c != '-'; c = getchar())
    ;
  if (c == '-') {
    neg = true;
    c = getchar();
  }
  while ((c >= '0' && c <= '9')) {
    n = n * 10 + c - '0';
    c = getchar();
  }
  return neg ? -n : n;
}
const int MAXC = 201;
const int MAXN = 100;
const int MAXM = 51;
int n;
string camino;
int dp[MAXC][MAXN][MAXM][2];
int go(int a, int b, int c, int d) {
  int i;
  int aa, dd;
  if (b == n) {
    return (c == 0) ? abs(a - 100) : -0x3f3f3f3f;
  }
  int& best = dp[a][b][c][d];
  if (best != -1) {
    return best;
  }
  best = -0x3f3f3f3f;
  for (i = (0); i < (c + 1); i++) {
    if (!(i & 1)) {
      aa = a + ((d == 0) ? 1 : -1) * ((camino[b] == 'F') ? 1 : 0);
      dd = (camino[b] == 'T') ? !d : d;
      best = max(best, go(aa, b + 1, c - i, dd));
    } else {
      aa = a + ((d == 0) ? 1 : -1) * ((camino[b] == 'F') ? 0 : 1);
      dd = (camino[b] == 'T') ? d : !d;
      best = max(best, go(aa, b + 1, c - i, dd));
    }
  }
  return best;
}
int main() {
  int m;
  cin >> camino;
  cin >> m;
  n = (int)camino.size();
  memset(dp, -1, sizeof dp);
  cout << go(100, 0, m, 0) << endl;
  return 0;
}
