//送出程式碼得到60%,我覺得可能是題目字串總長度超過存取上限65534
//這是送出程式碼得到的回覆,我不是很清楚其中的意思
/*
系統呼叫了 abort 函式! terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Aborted (core dumped)
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
int repeat(string ss[],int i,int re,string s[])
{
int u=i;
for(int j=0;j<re;j++){
i=u;
while(ss[0][i]!='E'){
if(ss[0][i]=='L'){
i++;
int rer=ss[0][i]-'0';
i++;
i=repeat(ss,i,rer,s);
}else if(ss[0][i]=='T'){
s[0]+=ss[0][i];
i++;
s[0]+=ss[0][i];
i++;
s[0]+=ss[0][i];
i++;
}
}
}
i++;
return i;
}
int main(void)
{
string ss[1]={};
string s[1]={};
getline(cin,ss[0]);
for(int i=0;i<ss[0].length();){
if(ss[0][i]=='L'){
i++;
int re=ss[0][i]-'0';
i++;
i=repeat(ss,i,re,s);
}else if(ss[0][i]=='T'){
s[0]+=ss[0][i];
i++;
s[0]+=ss[0][i];
i++;
s[0]+=ss[0][i];
i++;
}
}
int L=s[0].length();
char c,x1,x2,X,Y;
long long sum=0;
int h=0;
c=s[0][0];
x1=s[0][1];
x2=s[0][2];
Y=(x1-'0')*10+(x2-'0');
for(int i=3;i<L;){
c=s[0][i];
i++;
x1=s[0][i];
i++;
x2=s[0][i];
i++;
X=(x1-'0')*10+(x2-'0');
sum+=abs(X-Y);
Y=X;
}
cout<<sum;
return 0;
}