#include <bits/stdc++.h>
using namespace std;
#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:16777216")
#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:16777216")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
const int inf = 2000000000;
const long long linf = 5000000000000000000;
const long double LDINF = 2e18;
template <typename T>
void print(vector<T>& a) {
  for (int i = 0; i < a.size(); i++) cout << a[i] << ' ';
}
template <typename T>
void print(deque<T>& a) {
  for (int i = 0; i < a.size(); i++) cout << a[i] << ' ';
}
template <typename T>
void print(vector<vector<T>>& a) {
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[i].size(); j++) cout << a[i][j] << ' ';
    cout << '\n';
  }
}
template <typename T>
void input(vector<T>& a) {
  for (int i = 0; i < a.size(); i++) cin >> a[i];
}
template <typename T>
void input(deque<T>& a) {
  for (int i = 0; i < a.size(); i++) cin >> a[i];
}
template <typename T>
void input(vector<vector<T>>& a) {
  for (int i = 0; i < a.size(); i++)
    for (int j = 0; j < a[i].size(); j++) cin >> a[i][j];
}
long long mod = 1190494759;
int mod1 = 1e9 + 7;
long long p = 103;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
vector<vector<long long>> mult(vector<vector<long long>>& A,
                               vector<vector<long long>>& B) {
  int n = A.size(), m = B[0].size();
  vector<vector<long long>> C(n, vector<long long>(m, 0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < A[0].size(); k++) {
        C[i][j] += A[i][k] * B[k][j];
        C[i][j] %= mod;
      }
    }
  }
  return C;
}
long long BinPow(long long a, long long n) {
  if (n == 1) return a;
  if (n == 0) return 1;
  if (n % 2 == 0) {
    long long d = BinPow(a, n / 2);
    return d * d % mod;
  } else
    return a * BinPow(a, n - 1) % mod;
}
struct event {
  int num, type, x;
};
bool cmp(event& a, event& b) {
  if (a.x == b.x) {
    return a.type > b.type;
  }
  return a.x < b.x;
}
int Go(int val, int state) {
  if (state == 0)
    val--;
  else
    val++;
  return val;
}
int newState(int state) { return abs(state - 1); }
void solve() {
  string str;
  cin >> str;
  int len = str.length();
  int n;
  cin >> n;
  vector<vector<int>> dp(n + 1, vector<int>(2, -100500)),
      dp2(n + 1, vector<int>(2, 100500));
  dp[0][1] = 0;
  dp2[0][1] = 0;
  for (int i = 0; i < len; i++) {
    vector<vector<int>> newdp(n + 1, vector<int>(2, -100500));
    vector<vector<int>> newdp2(n + 1, vector<int>(2, 100500));
    for (int changes = 0; changes <= n; changes++) {
      for (int state = 0; state <= 1; state++) {
        if (dp[changes][state] == -100500) continue;
        if (changes != n) {
          if (str[i] == 'T') {
            int newval = Go(dp[changes][state], state);
            newdp[changes + 1][state] = max(newdp[changes + 1][state], newval);
            newval = Go(dp2[changes][state], state);
            newdp2[changes + 1][state] =
                min(newdp2[changes + 1][state], newval);
          } else {
            newdp[changes + 1][newState(state)] =
                max(newdp[changes + 1][newState(state)], dp[changes][state]);
            newdp2[changes + 1][newState(state)] =
                min(newdp2[changes + 1][newState(state)], dp2[changes][state]);
          }
        }
        if (str[i] == 'T') {
          newdp[changes][newState(state)] =
              max(newdp[changes][newState(state)], dp[changes][state]);
          newdp2[changes][newState(state)] =
              min(newdp2[changes][newState(state)], dp2[changes][state]);
        } else {
          int newval = Go(dp[changes][state], state);
          newdp[changes][state] = max(newdp[changes][state], newval);
          newval = Go(dp2[changes][state], state);
          newdp2[changes][state] = min(newdp2[changes][state], newval);
        }
      }
    }
    dp = newdp;
    dp2 = newdp2;
  }
  int ans = 0;
  for (int changes = 0; changes <= n; changes++) {
    if ((n - changes) % 2 == 1) continue;
    for (int state = 0; state <= 1; state++) {
      if (abs(dp[changes][state]) < 100500) ans = max(ans, dp[changes][state]);
      if (abs(dp2[changes][state]) < 100500)
        ans = max(ans, abs(dp2[changes][state]));
    }
  }
  cout << ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tst = 1;
  while (tst--) solve();
}
