main=print =<< solve <$> getLine

solve x@('T':xr) = sol (length $ takeWhile (=='T') x) (dropWhile (=='T') xr)
solve x = sol 0 x

sol a "" = a
sol a x
    | ns==0 || nt==0 = a + length x
    | ns == nt  = sol a xx
    | ns < nt   = sol (a+nt-ns) xx
    | otherwise = sol a ((replicate (ns-nt) 'S')++xx)
    where
        (xs,xr) = span (=='S') x
        (xt,xx) = span (=='T') xr
        ns = length xs
        nt = length xt