#include <bits/stdc++.h>
using namespace std;
long long int pw(long long int a, long long int b) {
  if (b == 0) return 1;
  long long int x = pw(a, b / 2);
  x = (x * x) % 1000000007;
  if (b % 2) x = (x * a) % 1000000007;
  return x;
}
unsigned int gcd(unsigned int u, unsigned int v) {
  int shift;
  if (u == 0) return v;
  if (v == 0) return u;
  shift = __builtin_ctz(u | v);
  u >>= __builtin_ctz(u);
  do {
    v >>= __builtin_ctz(v);
    if (u > v) {
      unsigned int t = v;
      v = u;
      u = t;
    }
    v = v - u;
  } while (v != 0);
  return u << shift;
}
long long int modPow(long long int a, long long int x, long long int p) {
  long long int res = 1;
  while (x > 0) {
    if (x % 2 != 0) {
      res = (res * a) % p;
    }
    a = (a * a) % p;
    x /= 2;
  }
  return res;
}
long long int modInverse(long long int a, long long int p) {
  return modPow(a, p - 2, p);
}
long long int ncr(long long int n, long long int k, long long int p) {
  if (k > n - k) return ncr(n, n - k, p);
  long long int numerator = 1;
  for (long long int i = 0; i < k; i++) {
    numerator = (numerator * (n - i)) % p;
  }
  long long int denominator = 1;
  for (long long int i = 1; i <= k; i++) {
    denominator = (denominator * i) % p;
  }
  return (numerator * modInverse(denominator, p)) % p;
}
string s;
int ans;
bool dp[501][2][101][1020];
void fl(int i, int f, int n, int val) {
  if (dp[i][f][n][val + 501]) return;
  dp[i][f][n][val + 501] = 1;
  if (i >= s.size()) {
    if (val < 0) val = val * -1;
    if (n % 2 == 0) ans = max(ans, val);
    return;
  }
  if (f == 1 && s[i] == 'F') {
    fl(i + 1, 1, n, val + 1);
    if (n > 0) {
      fl(i + 1, 0, n - 1, val);
    }
  }
  if (f == 1 && s[i] == 'T') {
    fl(i + 1, 0, n, val);
    if (n > 0) {
      fl(i + 1, 1, n - 1, val + 1);
    }
  }
  if (f == 0 && s[i] == 'F') {
    fl(i + 1, 0, n, val - 1);
    if (n > 0) {
      fl(i + 1, 1, n - 1, val);
    }
  }
  if (f == 0 && s[i] == 'T') {
    fl(i + 1, 1, n, val);
    if (n > 0) {
      fl(i + 1, 0, n - 1, val - 1);
    }
  }
}
int main(int argc, char const *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  for (long long int i = (0); i < (501); i++)
    for (long long int j = (0); j < (2); j++)
      for (long long int k = (0); k < (101); k++)
        for (long long int m = (0); m < (1020); m++) dp[i][j][k][m] = 0;
  ans = 0;
  int n;
  cin >> s;
  cin >> n;
  fl(0, 1, n, 0);
  cout << ans << endl;
  return 0;
}
