#include <bits/stdc++.h>
using namespace std;
int MAX = 105;
int dp[210][105][55][2];
int vis[210][105][55][2];
int n;
string s;
int go(int curr, int idx, int rem, int dir) {
  if (idx == s.size()) {
    if (rem % 2 == 1) {
      return INT_MIN;
    } else {
      return abs(curr - MAX);
    }
  }
  if (vis[curr][idx][rem][dir]) {
    return dp[curr][idx][rem][dir];
  }
  vis[curr][idx][rem][dir] = 1;
  int ans = INT_MIN;
  if (s[idx] == 'F') {
    int add = 1;
    if (dir == 1) {
      add = -1;
    }
    ans = max(ans, go(curr + add, idx + 1, rem, dir));
    if (rem) {
      ans = max(ans, go(curr, idx + 1, rem - 1, dir ^ 1));
    }
  } else {
    ans = max(ans, go(curr, idx + 1, rem, dir ^ 1));
    if (rem) {
      int add = 1;
      if (dir == 1) {
        add = -1;
      }
      ans = max(ans, go(curr + add, idx + 1, rem - 1, dir));
    }
  }
  return dp[curr][idx][rem][dir] = ans;
}
int main() {
  cin >> s;
  cin >> n;
  cout << go(MAX, 0, n, 0);
}
