#include <bits/stdc++.h>
using namespace std;
const int N = 55;
int dp[N << 1][N << 2][N][2] = {0};
char c[N << 1];
int n, l, ans = 0;
int dfs(int x, int cnt_t, int sum_t, int now) {
  if (cnt_t > n) return 0;
  if (x == l) {
    ans = max(ans, abs(now) - ((n - cnt_t) & 1));
    return ans;
  }
  int &sum = dp[x][now + 100][n - cnt_t][(sum_t - cnt_t) & 1];
  if (sum) return sum;
  if (c[x] == 'F') {
    int m = ((sum_t - cnt_t) & 1) ? -1 : 1;
    return sum = dfs(x + 1, cnt_t, sum_t, now + m);
  }
  if (c[x] == 'T') {
    int ans1 = dfs(x + 1, cnt_t, sum_t + 1, now);
    int m = ((sum_t - cnt_t) & 1) ? -1 : 1;
    int ans2 = dfs(x + 1, cnt_t + 1, sum_t + 1, now + m);
    return sum = max(ans1, ans2);
  }
}
int main() {
  scanf("%s%d", c, &n);
  l = strlen(c);
  dfs(0, 0, 0, 0);
  printf("%d\n", ans);
  return 0;
}
