#include <bits/stdc++.h>
using namespace std;
template <class TT>
TT abs(TT a) {
  if (a < 0) return -a;
  return a;
}
template <class ZZ>
ZZ max(ZZ a, ZZ b, ZZ c) {
  return max(a, max(b, c));
}
template <class ZZ>
ZZ min(ZZ a, ZZ b, ZZ c) {
  return min(a, min(b, c));
}
bool f[110][55];
pair<int, int> mm[110][55];
string s;
int l;
pair<int, int> go(int c, int r) {
  if (c == l) {
    return pair<int, int>(0, 0);
  }
  pair<int, int> &re = mm[c][r];
  if (f[c][r]) return re;
  f[c][r] = true;
  re = pair<int, int>(-110, 110);
  pair<int, int> cur;
  int i, t;
  t = 1;
  if (s[c] == 'T') t = -1;
  for (i = 0; i <= r; i++, t *= -1) {
    if (l - 1 != c || r - i == 0)
      ;
    else
      continue;
    cur = go(c + 1, r - i);
    if (t == 1) {
      re.first = max(re.first, 1 + max(cur.first, cur.second));
      re.second = min(re.second, 1 + min(cur.first, cur.second));
    } else {
      re.first = max(re.first, -min(cur.first, cur.second));
      re.second = min(re.second, -max(cur.first, cur.second));
    }
  }
  return re;
}
int main() {
  int i, j, k, tks, ks = 1, n, m;
  pair<int, int> p;
  while (cin >> s) {
    cin >> n;
    l = ((int)s.size());
    memset(f, false, sizeof(f));
    p = go(0, n);
    cout << max(abs(p.first), abs(p.second)) << endl;
  }
  return 0;
}
