#include <bits/stdc++.h>
using namespace std;
template <class t>
t abs2(const t &x) {
  return (x < 0 ? -x : x);
}
template <class t>
t max2(const t &x, const t &y) {
  return (x > y ? x : y);
}
template <class t>
t min2(const t &x, const t &y) {
  return (x < y ? x : y);
}
char s[105];
int mem[201][101][50][2];
int get(int p, int i, int k, bool d) {
  if (k < 0) return 0;
  if (s[i] == 0) return (k == 0 ? abs2(p) : 0);
  int &res = mem[p + 100][i][k][d];
  if (res != -1) return res;
  int mv = (d ? 1 : -1);
  res = max(get(p + mv, i + 1, k - (s[i] != 'F'), d),
            get(p, i + 1, k - (s[i] != 'T'), !d));
  return res;
}
int main() {
  memset(mem, -1, sizeof(mem));
  int K;
  cin >> s >> K;
  int mx = 0;
  for (int i = K; i >= 0; i -= 2) mx = max(mx, get(0, 0, i, true));
  cout << mx << endl;
  return 0;
}
