#include <bits/stdc++.h>
using namespace std;
#define repex(i, a, b, c) for(int i = a; i < b; i += c)
#define repx(i, a, b) repex(i, a, b, 1)
#define rep(i, n) repx(i, 0, n)
#define repr(i, a, b) for(int i = a; i >= b; i--)

int main(){
    
    // 1. 入力情報.
    char X[202020];
    scanf("%s", X);
    int l = strlen(X);
    
    // 2. 'T' が 来たら, stack から 'S' を 取り除く.
    stack<char> st;
    rep(i, l){
        char c = X[i];
        if(st.empty()){
            st.push(c);
            continue;
        }
        char t = st.top();
        if(c == 'T' && t == 'S') st.pop();
        else                     st.push(c);
    }
    
    // 3. 出力.
    printf("%d\n", st.size());
    return 0;
    
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ve vector

int main(){
    string s;
    cin >> s;
    ll n = s.size();
    reverse(s.begin(), s.end());
    ve<int> b(n+1);
    rep(i,n) b[i] = s[i] - '0';
    b[n] = 0;
    rep(i,n){
        if(b[i] >= 6 || (b[i] == 5 && b[i+1] >= 5)){
            b[i] = 10 - b[i];
            b[i+1]++;
        }
    }
    ll ans = 0;
    rep(i,n+1) ans += b[i];
    cout << ans << endl;
    return 0;
}