#include <bits/stdc++.h>
using namespace std;
string s;
int n, dp[105][105][2];
int delta(int id, int &x) {
  if (s[id] == 'F') return ((x == 0) ? 1 : -1);
  x = 1 - x;
  return 0;
}
int go(int id, int k, int x) {
  if (id == n && k == 0) return 0;
  if (id >= n || k < 0) return -10000000;
  int &mem = dp[id][k][x];
  if (mem != -1) return mem;
  int p1 = delta(id, x) + go(id + 1, k, x);
  delta(id, x);
  s[id] = (s[id] == 'F') ? 'T' : 'F';
  int p2 = go(id, k - 1, x);
  s[id] = (s[id] == 'F') ? 'T' : 'F';
  return mem = max(p1, p2);
}
int main() {
  int k;
  cin >> s >> k;
  n = s.size();
  memset(dp, -1, sizeof dp);
  int p1 = go(0, k, 0);
  memset(dp, -1, sizeof dp);
  int p2 = go(0, k, 1);
  cout << max(p1, p2) << endl;
}
