#include <bits/stdc++.h>
using namespace std;
using mll = map<long long, long long>;
using pll = pair<long long, long long>;
using lll = pair<long long, pll>;
using qll = queue<long long>;
using sll = set<long long>;
using vll = vector<long long>;
using vvll = vector<vector<long long>>;
using vvvll = vector<vector<vector<long long>>>;
using minpq = priority_queue<long long, vll, greater<long long>>;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
const long long MINLL = -1000;
const long long MAXLL = 1000;
int main() {
  string s;
  cin >> s;
  s = ' ' + s;
  long long k;
  scanf("%lld", &k);
  ++k;
  long long n = s.size();
  vvvll D(n, vvll(k, vll(2, MINLL)));
  D[0][0][0] = 0;
  long long ans = 0;
  for (long long i = 1; i < (n); ++i)
    for (long long j = 0; j < (k); ++j) {
      for (long long l = 0; l < (2); ++l)
        D[i][j][l] = max(
            j > 0 or s[i] == 'F' ? D[i - 1][j - (s[i] != 'F')][l] + (l ? 1 : -1)
                                 : MINLL,
            j > 0 or s[i] == 'T' ? D[i - 1][j - (s[i] != 'T')][l ^ 1] : MINLL);
      if (i == n - 1 and (k - j) % 2 == 1)
        ans = max(ans, *max_element(D[i][j].begin(), D[i][j].end()));
    }
  long long ans2 = 0;
  D = vvvll(n, vvll(k, vll(2, MAXLL)));
  D[0][0][0] = 0;
  for (long long i = 1; i < (n); ++i)
    for (long long j = 0; j < (k); ++j) {
      for (long long l = 0; l < (2); ++l)
        D[i][j][l] = min(
            j > 0 or s[i] == 'F' ? D[i - 1][j - (s[i] != 'F')][l] + (l ? 1 : -1)
                                 : MAXLL,
            j > 0 or s[i] == 'T' ? D[i - 1][j - (s[i] != 'T')][l ^ 1] : MAXLL);
      if (i == n - 1 and (k - j) % 2 == 1)
        ans2 = min(ans2, *min_element(D[i][j].begin(), D[i][j].end()));
    }
  printf("%lld\n", max(ans, -ans2));
}
