#include <bits/stdc++.h>
using namespace std;
const int INF = (1u << 31) - 1;
const int MN = 100 + 10;
string com;
pair<int, int> d[MN][MN];
bool mark[MN][MN];
int n;
char sw(char a) {
  if (a == 'F') return 'T';
  return 'F';
}
pair<int, int> fll(int a, int b) {
  if (mark[a][b]) return d[a][b];
  mark[a][b] = 1;
  if (a == com.size() - 1) {
    char t = com[a];
    if (b % 2 == 1) t = sw(t);
    if (t == 'F')
      return d[a][b] = pair<int, int>(1, 1);
    else
      return d[a][b] = pair<int, int>(0, 0);
  }
  bool first = true;
  for (int i = 0; i <= b; i++) {
    char t = com[a];
    int f, s;
    if (i % 2 == 1) t = sw(t);
    if (t == 'F')
      f = fll(a + 1, b - i).first + 1, s = fll(a + 1, b - i).second + 1;
    else
      f = -fll(a + 1, b - i).second, s = -fll(a + 1, b - i).first;
    if (first) {
      first = false;
      d[a][b] = pair<int, int>(f, s);
    } else {
      d[a][b].first = min(d[a][b].first, f);
      d[a][b].second = max(d[a][b].second, s);
    }
  }
  return d[a][b];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> com >> n;
  cout << max(abs(fll(0, n).first), abs(fll(0, n).second)) << endl;
  return 0;
}
