#include <bits/stdc++.h>
using namespace std;
int k;
string s;
bool a[105][55][205][3];
int ans = -1;
void go(int pos, int change, int x, int dx) {
  if (a[pos][change][x + 100][dx + 1]) {
    return;
  }
  a[pos][change][x + 100][dx + 1] = 1;
  if (pos == s.size()) {
    if (change == 0) {
      ans = max(ans, abs(x));
    }
    return;
  }
  for (int i = 0; i <= change; i++) {
    int val = (s[pos] == 'F');
    if (i % 2) {
      val = 1 - val;
    }
    go(pos + 1, change - i, x + (val == 1 ? dx : 0), dx * (val == 0 ? -1 : 1));
  }
}
int main() {
  cin >> s;
  cin >> k;
  go(0, k, 0, 1);
  cout << ans;
  return 0;
}
