#include <bits/stdc++.h>
using namespace std;
string s;
long long n;
long long change;
long long res = 0;
long long moved = 0;
long long dp[101][55][202];
void go(long long pos, long long change, long long moved) {
  if (pos == n) {
    res = max(res, abs(moved) - (change & 1));
    return;
  }
  if (dp[pos][change][moved]) return;
  dp[pos][change][moved] = 1;
  if (s[pos] == 'F') {
    go(pos + 1, change, moved + 1);
    if (change) {
      go(pos + 1, change - 1, -moved);
    }
  } else {
    go(pos + 1, change, -moved);
    if (change) {
      go(pos + 1, change - 1, moved + 1);
    }
  }
}
void solve() {
  memset(dp, 0, sizeof dp);
  cin >> s;
  n = (long long)s.size();
  cin >> change;
  go(0, change, 0);
  cout << res << endl;
}
signed main() {
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
