#include <bits/stdc++.h>
using namespace std;
int N, len;
int memo[110][55][4];
char s[110];
int solve(int curr, int left, bool pwn) {
  int ret = -1000000000;
  if (memo[curr][left][pwn] > -1000000000 - 100) return memo[curr][left][pwn];
  if (curr == len && left == 0) return 0;
  if (curr == len) return -1000000000;
  int step = 1;
  if (!pwn) step = -1;
  if (left > 0) {
    if (s[curr] == 'T') {
      for (int i = 1; i <= left; i += 2) {
        ret = max(ret, step + solve(curr + 1, left - i, pwn));
      }
      for (int i = 2; i <= left; i += 2) {
        ret = max(ret, solve(curr + 1, left - i, !pwn));
      }
    } else {
      for (int i = 1; i <= left; i += 2) {
        ret = max(ret, solve(curr + 1, left - i, !pwn));
      }
      for (int i = 2; i <= left; i += 2) {
        ret = max(ret, step + solve(curr + 1, left - i, pwn));
      }
    }
  }
  if (s[curr] == 'T') {
    ret = max(ret, solve(curr + 1, left, !pwn));
  } else {
    ret = max(ret, step + solve(curr + 1, left, pwn));
  }
  return memo[curr][left][pwn] = ret;
}
int solve1(int curr, int left, bool pwn) {
  int ret = 1000000000;
  if (memo[curr][left][pwn] < 1000000000 + 100) return memo[curr][left][pwn];
  if (curr == len && left == 0) return 0;
  if (curr == len) return 1000000000;
  int step = 1;
  if (!pwn) step = -1;
  if (left > 0) {
    if (s[curr] == 'T') {
      for (int i = 1; i <= left; i += 2) {
        ret = min(ret, step + solve1(curr + 1, left - i, pwn));
      }
      for (int i = 2; i <= left; i += 2) {
        ret = min(ret, solve1(curr + 1, left - i, !pwn));
      }
    } else {
      for (int i = 1; i <= left; i += 2) {
        ret = min(ret, solve1(curr + 1, left - i, !pwn));
      }
      for (int i = 2; i <= left; i += 2) {
        ret = min(ret, step + solve1(curr + 1, left - i, pwn));
      }
    }
  }
  if (s[curr] == 'T') {
    ret = min(ret, solve1(curr + 1, left, !pwn));
  } else {
    ret = min(ret, step + solve1(curr + 1, left, pwn));
  }
  return memo[curr][left][pwn] = ret;
}
int main(void) {
  scanf("%s", s);
  len = strlen(s);
  scanf("%d", &N);
  for (int i = 0; i < 110; ++i) {
    for (int j = 0; j < 55; ++j) {
      for (int k = 0; k < 2; ++k) memo[i][j][k] = -1000000000 - 100;
    }
  }
  int A = solve(0, N, 1);
  for (int i = 0; i < 110; ++i) {
    for (int j = 0; j < 55; ++j) {
      for (int k = 0; k < 2; ++k) memo[i][j][k] = 1000000000 + 100;
    }
  }
  int B = abs(solve1(0, N, 1));
  printf("%d\n", max(A, B));
  return 42 - 42;
}
