← Home
package main

import (
	"fmt"
)

func main() {
	var s string
	var n int
	fmt.Scan(&s, &n)

	l := len(s)
	dp := make([][][]bool, n+1)
	for j := 0; j <= n; j++ {
		dp[j] = make([][]bool, 2)
		for d := 0; d < 2; d++ {
			dp[j][d] = make([]bool, 220)
		}
	}

	dp[0][0][105] = true

	for i := 0; i < l; i++ {
		nextDp := make([][][]bool, n+1)
		for j := 0; j <= n; j++ {
			nextDp[j] = make([][]bool, 2)
			for d := 0; d < 2; d++ {
				nextDp[j][d] = make([]bool, 220)
			}
		}

		cmd := s[i]
		for j := 0; j <= n; j++ {
			for d := 0; d < 2; d++ {
				for p := 0; p < 220; p++ {
					if dp[j][d][p] {
						if cmd == 'F' {
							np := p + 1
							if d == 1 {
								np = p - 1
							}
							nextDp[j][d][np] = true
						} else {
							nextDp[j][1-d][p] = true
						}

						if j+1 <= n {
							if cmd == 'T' {
								np := p + 1
								if d == 1 {
									np = p - 1
								}
								nextDp[j+1][d][np] = true
							} else {
								nextDp[j+1][1-d][p] = true
							}
						}
					}
				}
			}
		}
		dp = nextDp
	}

	ans := 0
	for j := 0; j <= n; j++ {
		if (n-j)%2 == 0 {
			for d := 0; d < 2; d++ {
				for p := 0; p < 220; p++ {
					if dp[j][d][p] {
						dist := p - 105
						if dist < 0 {
							dist = -dist
						}
						if dist > ans {
							ans = dist
						}
					}
				}
			}
		}
	}

	fmt.Println(ans)
}