#include <bits/stdc++.h>
using namespace std;
string s;
int const INF = 10000009;
int N, dp[2][109][59];
bool done[2][109][59];
int F(int d) {
  if (d == 0)
    return +1;
  else
    return -1;
}
int go(int d, int i, int n) {
  if (n < 0) return -INF;
  if (i == s.size()) {
    if (n > 0)
      return -INF;
    else
      return 0;
  }
  if (done[d][i][n]) return dp[d][i][n];
  if (s[i] == 'T') {
    dp[d][i][n] = max({go(1 - d, i + 1, n), go(d, i + 1, n - 1) + F(d)});
  }
  if (s[i] == 'F') {
    dp[d][i][n] = max({go(d, i + 1, n) + F(d), go(1 - d, i + 1, n - 1)});
  }
  done[d][i][n] = true;
  return dp[d][i][n];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> s >> N;
  int ans = 0;
  for (int j = N; j >= 0; j -= 2) ans = max({ans, go(0, 0, j), go(1, 0, j)});
  cout << ans << '\n';
}
