#include <bits/stdc++.h>
using namespace std;
int k, n;
string x;
pair<int, int> memo[2][105][55];
pair<int, int> solve(int d, int idx, int rem) {
  if (idx == n) {
    if (rem % 2 == 0) return pair<int, int>(0, 0);
    return pair<int, int>(-1e8, -1e8);
  }
  pair<int, int> &ret = memo[d][idx][rem];
  if (ret.first != -1e9) return ret;
  if (d) {
    if (x[idx] == 'F') {
      ret = solve(d, idx + 1, rem);
      ret.first -= 1, ret.second += 1;
      if (rem) {
        pair<int, int> res = solve(0, idx + 1, rem - 1);
        ret.first = max(ret.first, res.first);
        ret.second = max(ret.second, res.second);
      }
    } else {
      ret = solve(0, idx + 1, rem);
      if (rem) {
        pair<int, int> res = solve(d, idx + 1, rem - 1);
        res.first -= 1, res.second += 1;
        ret.first = max(ret.first, res.first);
        ret.second = max(ret.second, res.second);
      }
    }
  } else {
    if (x[idx] == 'F') {
      ret = solve(d, idx + 1, rem);
      ret.first += 1, ret.second -= 1;
      if (rem) {
        pair<int, int> res = solve(1, idx + 1, rem - 1);
        ret.first = max(ret.first, res.first);
        ret.second = max(ret.second, res.second);
      }
    } else {
      ret = solve(1, idx + 1, rem);
      if (rem) {
        pair<int, int> res = solve(0, idx + 1, rem - 1);
        res.first += 1, res.second -= 1;
        ret.first = max(ret.first, res.first);
        ret.second = max(ret.second, res.second);
      }
    }
  }
  return ret;
}
int main() {
  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 105; ++j)
      for (int k = 0; k < 55; ++k) memo[i][j][k] = pair<int, int>(-1e9, -1e9);
  cin >> x;
  n = x.size();
  cin >> k;
  printf("%d\n", max(solve(1, 0, k).first, solve(1, 0, k).second));
}
