#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const long long infLL = 9000000000000000000;
inline bool checkBit(long long n, int i) { return n & (1LL << i); }
inline long long setBit(long long n, int i) {
  return n | (1LL << i);
  ;
}
inline long long resetBit(long long n, int i) { return n & (~(1LL << i)); }
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
inline bool isLeapYear(long long year) {
  return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
inline void normal(long long &a) {
  a %= (long long)1e18;
  (a < 0) && (a += (long long)1e18);
}
inline long long modMul(long long a, long long b) {
  a %= (long long)1e18, b %= (long long)1e18;
  normal(a), normal(b);
  return (a * b) % (long long)1e18;
}
inline long long modAdd(long long a, long long b) {
  a %= (long long)1e18, b %= (long long)1e18;
  normal(a), normal(b);
  return (a + b) % (long long)1e18;
}
inline long long modSub(long long a, long long b) {
  a %= (long long)1e18, b %= (long long)1e18;
  normal(a), normal(b);
  a -= b;
  normal(a);
  return a;
}
inline long long modPow(long long b, long long p) {
  long long r = 1;
  while (p) {
    if (p & 1) r *= b;
    b *= b;
    p >>= 1;
  }
  return r;
}
inline long long modInverse(long long a) {
  return modPow(a, (long long)1e18 - 2);
}
inline long long modDiv(long long a, long long b) {
  return modMul(a, modInverse(b));
}
int dp[110][300][2][55], n, len;
int dx[2] = {1, -1};
string s;
int func(int idx, int pos, int ori, int use) {
  if (use > n) return 0;
  if (idx == len) {
    if (use == n) return abs(pos - 150);
    return 0;
  }
  int &ret = dp[idx][pos][ori][use];
  if (ret != -1) return ret;
  ret = 0;
  if (s[idx] == 'T') {
    for (int i = use; i <= n; ++i) {
      if ((i - use) & 1)
        ret = max(ret, func(idx + 1, pos + dx[ori], ori, i));
      else
        ret = max(ret, func(idx + 1, pos, 1 - ori, i));
    }
  } else {
    for (int i = use; i <= n; ++i) {
      if ((i - use) & 1)
        ret = max(ret, func(idx + 1, pos, 1 - ori, i));
      else
        ret = max(ret, func(idx + 1, pos + dx[ori], ori, i));
    }
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> s >> n;
  len = s.size();
  memset(dp, -1, sizeof(dp));
  cout << func(0, 150, 0, 0);
  return 0;
}
