#include <bits/stdc++.h>
using namespace std;
string s;
int n;
int dp[101][51][2][210];
int solve(int index, int remainingChanges, int direction, int position) {
  if (remainingChanges < 0) return -10000;
  if (index == s.size()) {
    return remainingChanges % 2 == 0 ? abs(position) : -10000;
  }
  if (dp[index][remainingChanges][direction][position + 100] != -1)
    return dp[index][remainingChanges][direction][position + 100];
  if (s[index] == 'F') {
    int way1 = solve(index + 1, remainingChanges, direction,
                     position + (direction == 0 ? +1 : -1));
    int way2 = solve(index + 1, remainingChanges - 1, direction ^ 1, position);
    return dp[index][remainingChanges][direction][position + 100] =
               max(way1, way2);
  } else {
    int way1 = solve(index + 1, remainingChanges, direction ^ 1, position);
    int way2 = solve(index + 1, remainingChanges - 1, direction,
                     position + (direction == 0 ? +1 : -1));
    return dp[index][remainingChanges][direction][position + 100] =
               max(way1, way2);
  }
}
int main() {
  memset(dp, -1, sizeof dp);
  cin >> s >> n;
  cout << solve(0, n, 0, 0);
  return 0;
}
