#include <bits/stdc++.h>
using namespace std;
static const double EPS = 1e-10;
template <typename T, typename S>
vector<T>& operator<<(vector<T>& a, S b) {
  a.push_back(b);
  return a;
}
template <typename T>
void operator>>(vector<T>& a, int b) {
  while (b--)
    if (!a.empty()) a.pop_back();
}
bool isprime(int n) {
  if (n < 2) return false;
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0) return false;
  return true;
}
long long b_pow(long long x, long long n) {
  return n ? b_pow(x * x, n / 2) * (n % 2 ? x : 1) : 1ll;
}
string itos(int n) {
  stringstream ss;
  ss << n;
  return ss.str();
}
string line;
int p;
int memo[256][2][110][55];
struct P {
  int x, dir, pos, r;
  P(int x, int dir, int pos, int r) : x(x), dir(dir), pos(pos), r(r) {}
  P() {}
};
int main() {
  ios_base::sync_with_stdio(false);
  memset(memo, 0, sizeof(memo));
  cin >> line;
  cin >> p;
  queue<P> Q;
  Q.push(P(110, 0, 0, p));
  memo[110][0][0][p] = true;
  int d[] = {1, -1};
  P w;
  while (Q.size()) {
    P q = Q.front();
    Q.pop();
    if (q.pos == line.size()) continue;
    if (line[q.pos] == 'T') {
      for (int u = 0; u <= q.r; u += 2) {
        w = P(q.x, q.dir ^ 1, q.pos + 1, q.r - u);
        if (!memo[w.x][w.dir][w.pos][w.r])
          Q.push(w), memo[w.x][w.dir][w.pos][w.r] = true;
      }
      for (int u = 1; u <= q.r; u += 2) {
        w = P(q.x + d[q.dir], q.dir, q.pos + 1, q.r - u);
        if (!memo[w.x][w.dir][w.pos][w.r])
          Q.push(w), memo[w.x][w.dir][w.pos][w.r] = true;
      }
    } else {
      for (int u = 1; u <= q.r; u += 2) {
        w = P(q.x, q.dir ^ 1, q.pos + 1, q.r - u);
        if (!memo[w.x][w.dir][w.pos][w.r])
          Q.push(w), memo[w.x][w.dir][w.pos][w.r] = true;
      }
      for (int u = 0; u <= q.r; u += 2) {
        w = P(q.x + d[q.dir], q.dir, q.pos + 1, q.r - u);
        if (!memo[w.x][w.dir][w.pos][w.r])
          Q.push(w), memo[w.x][w.dir][w.pos][w.r] = true;
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < 256; i++) {
    for (int j = 0; j < 2; j++) {
      if (memo[i][j][line.size()][0]) ans = max(ans, abs(i - 110));
    }
  }
  cout << ans << endl;
}
