#include <bits/stdc++.h>
using namespace std;
bool debug = 0;
string command;
int dp[105][2][51][205][2];
void init() {
  for (int i = 0; i < 105; i++) {
    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < 51; k++) {
        for (int l = 0; l < 205; l++) {
          for (int m = 0; m < 2; m++) {
            dp[i][j][k][l][m] = -1000000007;
          }
        }
      }
    }
  }
}
int cToInt(char c) { return (c == 'F') ? 0 : 1; }
int bToI(bool b) { return (b) ? 1 : 0; }
int dToI(int distance) { return distance + 100; }
int solve(int idx, char cmd, int changeLeft, int distance, bool isKeKanan) {
  if (debug) {
    cout << "idx"
         << ": " << idx << endl;
  }
  if (debug) {
    cout << "cmd"
         << ": " << cmd << endl;
  }
  if (debug) {
    cout << "changeLeft"
         << ": " << changeLeft << endl;
  }
  if (debug) {
    cout << "distance"
         << ": " << distance << endl;
  }
  if (debug) {
    cout << "isKeKanan"
         << ": " << isKeKanan << endl;
  }
  if (idx == command.size() - 1) {
    if (changeLeft == 0) {
      if (cmd == 'F') {
        return distance + (isKeKanan ? 1 : -1);
      } else {
        return distance;
      }
    } else {
      if (dp[idx][cToInt(cmd)][changeLeft][dToI(distance)][bToI(isKeKanan)] !=
          -1000000007) {
        return dp[idx][cToInt(cmd)][changeLeft][dToI(distance)]
                 [bToI(isKeKanan)];
      } else {
        return dp[idx][cToInt(cmd)][changeLeft][dToI(distance)]
                 [bToI(isKeKanan)] = solve(idx, (cmd == 'F') ? 'T' : 'F',
                                           changeLeft - 1, distance, isKeKanan);
      }
    }
  } else if (dp[idx][cToInt(cmd)][changeLeft][dToI(distance)]
               [bToI(isKeKanan)] != -1000000007) {
    if (debug) {
      cout << "\"memo\""
           << ": "
           << "memo" << endl;
    }
    return dp[idx][cToInt(cmd)][changeLeft][dToI(distance)][bToI(isKeKanan)];
  } else {
    int maxi = -1000000007;
    if (cmd == 'F') {
      maxi = max(maxi, solve(idx + 1, command[idx + 1], changeLeft,
                             distance + ((isKeKanan) ? 1 : -1), isKeKanan));
    } else {
      maxi = max(maxi, solve(idx + 1, command[idx + 1], changeLeft, distance,
                             !isKeKanan));
    }
    if (changeLeft > 0) {
      if (cmd == 'F') {
        maxi = max(maxi, solve(idx, 'T', changeLeft - 1, distance, isKeKanan));
      } else {
        maxi = max(maxi, solve(idx, 'F', changeLeft - 1, distance, isKeKanan));
      }
    }
    if (debug) {
      cout << "maxi"
           << ": " << maxi << endl;
    }
    return dp[idx][cToInt(cmd)][changeLeft][dToI(distance)][bToI(isKeKanan)] =
               maxi;
  }
}
int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> command;
  cin >> n;
  init();
  int kanan = solve(0, command[0], n, 0, true);
  int kiri = solve(0, command[0], n, 0, false);
  cout << max(kanan, kiri) << endl;
  return 0;
}
