#include <bits/stdc++.h>
using namespace std;
bool t[53][103][206][3] = {false};
int ans = 0;
string s;
int a(int c) {
  if (c > 0)
    return c;
  else
    return (-1) * c;
}
void f(int i, int j, int dis, int d) {
  if (t[i][j][dis][d]) return;
  t[i][j][dis][d] = true;
  if (a(103 - dis) > ans && i % 2 == 0 && j == s.size()) ans = a(103 - dis);
  if (j == s.size()) return;
  if (s[j] == 'T') {
    if (i > 0) f(i - 1, j + 1, dis + (d - 1), d);
    f(i, j + 1, dis, ((-1) * (d - 1) + 1));
  } else {
    if (i > 0) f(i - 1, j + 1, dis, ((-1) * (d - 1) + 1));
    f(i, j + 1, dis + (d - 1), d);
  }
}
int main() {
  cin >> s;
  int n = 0;
  cin >> n;
  f(n, 0, 103, 2);
  f(n, 0, 103, 0);
  cout << ans << endl;
  return 0;
}
