#include <bits/stdc++.h>
using namespace std;
int n, c, t, dp[111][55][3];
vector<int> v;
string s;
int f(int cur, int c, int inv) {
  int& res = dp[cur][c][inv];
  if (res != -1000000007LL) return res;
  if (cur == t) {
    if (c % 2 == 0) return (inv ? -v[cur] : v[cur]);
    return (inv ? -v[cur] + 1 : v[cur] - 1);
  }
  if (c) {
    res = f(cur + 1, c - 1, inv) + (inv ? -1 : 1);
  }
  res = max(res, f(cur + 1, c, inv ^ 1));
  res += (inv ? -v[cur] : v[cur]);
  return res;
}
int main() {
  cin >> s >> n;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'F')
      c++;
    else {
      v.push_back(c);
      t++;
      c = 0;
    }
  }
  v.push_back(c);
  if (n >= t) {
    cout << s.size() - (n - t) % 2;
    return 0;
  }
  for (int i = 0; i < 111; i++)
    for (int j = 0; j < 55; j++)
      for (int k = 0; k < 2; k++) dp[i][j][k] = -1000000007LL;
  int res = f(0, n, 0);
  for (int i = 0; i < 111; i++)
    for (int j = 0; j < 55; j++)
      for (int k = 0; k < 2; k++) dp[i][j][k] = -1000000007LL;
  cout << max(res, f(0, n, 1));
}
