#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
string s;
int n, ans = 0;
bool vis[110][410][60][2];
void dfs(int spos, int pos, int lft, int d) {
  if (vis[spos][pos][lft][d]) return;
  vis[spos][pos][lft][d] = true;
  if (lft == 0 && spos == s.size()) ans = max(ans, abs(pos - 200));
  if (spos == s.size()) return;
  if (spos == s.size()) return;
  for (int i = 0; i < lft + 1; i++) {
    int isf = s[spos] == 'F' ? 1 : 0;
    if (i % 2 == 1) isf ^= 1;
    dfs(spos + 1, pos + isf * (d == 0 ? 1 : -1), lft - i, isf ? d : d ^ 1);
  }
}
int main() {
  cin >> s >> n;
  dfs(0, 200, n, 0);
  cout << ans << endl;
  return 0;
}
