#include <bits/stdc++.h>
using namespace std;
int calc(string s) {
  int d = 1;
  int res = 0;
  for (int i = 0; i < s.length(); i++)
    if (s[i] == 'F')
      res += d;
    else
      d = -d;
  return abs(res);
}
pair<int, int> dp[101][101][51];
int main() {
  string s;
  cin >> s;
  int n;
  cin >> n;
  int max = 0;
  if (n % 2 == 0)
    max = calc(s);
  else {
    string ns = s;
    for (int i = 0; i < ns.length(); i++) {
      if (ns[i] == 'T')
        ns[i] = 'F';
      else
        ns[i] = 'T';
      int tmp = calc(ns);
      if (tmp > max) {
        max = tmp;
        s = ns;
      }
      if (ns[i] == 'T')
        ns[i] = 'F';
      else
        ns[i] = 'T';
    }
  }
  for (int i = 0; i < n / 2; i++) {
    int cmax = 0;
    string ns = s;
    for (int j = 0; j < ns.length(); j++)
      for (int k = j + 1; k < ns.length(); k++) {
        if (ns[j] == 'T')
          ns[j] = 'F';
        else
          ns[j] = 'T';
        if (ns[k] == 'T')
          ns[k] = 'F';
        else
          ns[k] = 'T';
        int tmp = calc(ns);
        if (tmp > cmax) {
          cmax = tmp;
          s = ns;
        }
        if (ns[j] == 'T')
          ns[j] = 'F';
        else
          ns[j] = 'T';
        if (ns[k] == 'T')
          ns[k] = 'F';
        else
          ns[k] = 'T';
      }
    if (cmax > max) max = cmax;
  }
  cout << max;
  return 0;
}
