#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long dx[] = {-1, 0, 1, 0};
long long dy[] = {0, -1, 0, 1};
bool vis[2][210][110][55];
string s;
long long n;
void dfs(long long dir, long long pos, long long in, long long ch) {
  if (vis[dir][pos][in][ch]) {
    return;
  }
  vis[dir][pos][in][ch] = true;
  long long x = (dir == 0 ? -1 : 1);
  if (in == s.size()) {
    return;
  }
  if (s[in] == 'T') {
    dfs(1 - dir, pos, in + 1, ch);
  } else {
    dfs(dir, pos + x, in + 1, ch);
  }
  if (ch + 1 <= n) {
    if (s[in] == 'T') {
      dfs(dir, pos + x, in + 1, ch + 1);
    } else {
      dfs(1 - dir, pos, in + 1, ch + 1);
    }
  }
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> s >> n;
  dfs(0, 102, 0, 0);
  long long res = 0;
  for (long long i = 0; i < 2; i++) {
    for (long long j = 0; j < 210; j++) {
      for (long long k = n; k >= 0; k -= 2) {
        if (vis[i][j][s.size()][k] == 1) {
          res = max(res, abs(102 - j));
        }
      }
    }
  }
  cout << res << "\n";
}
