using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tencent_5
{
class Program
{
static void Main(string[] args)
{
string s = Console.ReadLine();
int n=Convert.ToInt32(Console.ReadLine());
int[,,] dp = new int[s.Length, n + 1, 2];
for(int i=1;i<s.Length;i++)
for(int j=0;j<n+1;j++)
{
dp[i, j, 0] = 0;
dp[i, j, 1] = 0;
}
if (s[0] == 'F')
{
for (int i = 0; i <= n; i++)
{
if (i % 2 == 1)
{
dp[0, i, 0] = 0;
dp[0, i, 1] = -1;
}
else
{
dp[0, i, 0] = 1;
dp[0, i, 1] = 1;
}
}
}
else
{
for (int i = 0; i <= n; i++)
{
if (i % 2 == 1)
{
dp[0, i, 0] = 1;
dp[0, i, 1] = 1;
}
else
{
dp[0, i, 0] = 0;
dp[0, i, 1] = -1;
}
}
}
for (int i = 1; i < s.Length; i++)
for (int j = 0; j <= n; j++)
for (int k = 0; k <= j; k++)
{
if(s[i]=='F')
{
if((j-k)%2==0)
{
if(dp[i-1,k,0]+ dp[i - 1, k, 1] > dp[i,j,0])
{
dp[i, j, 0] = dp[i - 1, k, 0] + dp[i - 1, k, 1];
dp[i, j, 1] = dp[i - 1, k, 1];
}
}
else
{
if (dp[i - 1, k, 0] > dp[i, j, 0])
{
dp[i, j, 0] = dp[i - 1, k, 0];
dp[i, j, 1] = dp[i - 1, k, 1] * -1;
}
}
}
else
{
if ((j - k) % 2 == 0)
{
if (dp[i - 1, k, 0] > dp[i, j, 0])
{
dp[i, j, 0] = dp[i - 1, k, 0];
dp[i, j, 1] = dp[i - 1, k, 1] * -1;
}
}
else
{
if (dp[i - 1, k, 0] + dp[i - 1, k, 1] > dp[i, j, 0])
{
dp[i, j, 0] = dp[i - 1, k, 0] + dp[i - 1, k, 1];
dp[i, j, 1] = dp[i - 1, k, 1];
}
}
}
}
int positiveans = dp[s.Length - 1, n, 0];
for (int i = 1; i < s.Length; i++)
for (int j = 0; j < n + 1; j++)
{
dp[i, j, 0] = 0;
dp[i, j, 1] = 0;
}
for (int i = 1; i < s.Length; i++)
for (int j = 0; j <= n; j++)
for (int k = 0; k <= j; k++)
{
if (s[i] == 'F')
{
if ((j - k) % 2 == 0)
{
if (dp[i - 1, k, 0] + dp[i - 1, k, 1] < dp[i, j, 0])
{
dp[i, j, 0] = dp[i - 1, k, 0] + dp[i - 1, k, 1];
dp[i, j, 1] = dp[i - 1, k, 1];
}
}
else
{
if (dp[i - 1, k, 0] < dp[i, j, 0])
{
dp[i, j, 0] = dp[i - 1, k, 0];
dp[i, j, 1] = dp[i - 1, k, 1] * -1;
}
}
}
else
{
if ((j - k) % 2 == 0)
{
if (dp[i - 1, k, 0] < dp[i, j, 0])
{
dp[i, j, 0] = dp[i - 1, k, 0];
dp[i, j, 1] = dp[i - 1, k, 1] * -1;
}
}
else
{
if (dp[i - 1, k, 0] + dp[i - 1, k, 1] < dp[i, j, 0])
{
dp[i, j, 0] = dp[i - 1, k, 0] + dp[i - 1, k, 1];
dp[i, j, 1] = dp[i - 1, k, 1];
}
}
}
}
int negativeans = dp[s.Length - 1, n, 0];
Console.WriteLine(Math.Max(positiveans,Math.Abs(negativeans)));
Console.ReadKey();
}
}
}