#include <bits/stdc++.h>
using namespace std;
priority_queue<char, vector<char>, greater<char> > pq;
double startTime;
double getCurrentTime() {
  return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
bool isPrime(long long int n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (long long int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
long long int gcd(long long int a, long long int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long int power(long long int x, unsigned long long int y,
                    long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
string s;
long long int n, m;
long long int shift = 105;
long long int dp[105][55][250][2];
long long int solve(long long int idx, long long int moves, long long int pos,
                    long long int dir) {
  if (idx == n) {
    if (moves == 0) return abs(pos);
    return 0;
  }
  if (dp[idx][moves][pos + shift][dir] != -1)
    return dp[idx][moves][pos + shift][dir];
  long long int sh = 1, ret = 0;
  if (dir == 1) sh = -1;
  if (s[idx] == 'F') {
    ret = solve(idx + 1, moves, pos + sh, dir);
    if (moves > 0) ret = max(ret, solve(idx + 1, moves - 1, pos, 1 - dir));
    if (moves >= 2) ret = max(ret, solve(idx, moves - 2, pos, dir));
  } else {
    ret = solve(idx + 1, moves, pos, 1 - dir);
    if (moves > 0) ret = max(ret, solve(idx + 1, moves - 1, pos + sh, dir));
    if (moves >= 2) ret = max(ret, solve(idx, moves - 2, pos, dir));
  }
  return dp[idx][moves][pos + shift][dir] = ret;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  memset(dp, -1, sizeof(dp));
  cin >> s >> m;
  n = s.size();
  cout << solve(0, m, 0, 0);
  return 0;
}
