Example

Input

R?????,2?)


Output

29
#include<bits/stdc++.h>
using namespace std;

using ll=string;
ll to_ll(string exp){
    ll res="";
    bool top0=false;
    for(int i=0;i<exp.size();i++){
        if(top0) return "-1";
        if(exp[i]=='?'){
            res+=('9');
        }
        else if(isdigit(exp[i])){
            res+=exp[i];
            top0=(i==0 && exp[i]=='0');
        }
        else{
            return "-1";
        }
    }
    return res;
}

bool isGood(char x,char y){
    return x==y || x=='?';
}
bool isOK(const string& exp,int p,char com){
    bool comb=isGood(exp[0],com);
    bool parb=isGood(exp[1],'(') && isGood(exp.back(),')');
    bool commab=isGood(exp[p],',');
    return comb && parb && commab;
}

string mymax(string l,string r){
    if(l=="-1") return r;
    if(r=="-1") return l;
    if(l.size()<r.size()) return r;
    if(r.size()<l.size()) return l;
    return max(l,r);
}
int main(){
    string s;
    cin>>s;
    int n=s.size();
    vector<vector<ll>> dp(n,vector<ll>(n+1,"-1"));
    for(int k=1;k<=n;k++){
        for(int l=0;l+k<=n;l++){
            string exp=s.substr(l,k);
            ll res=to_ll(exp);
            if(k>=6){
                for(int p=3;p+2<k;p++){
                    if(isOK(exp,p,'L') && dp[l+2][l+p]!="-1" && dp[l+p+1][l+k-1]!="-1"){
                        res=mymax(res,dp[l+2][l+p]);
                    }
                    if(isOK(exp,p,'R') && dp[l+2][l+p]!="-1" && dp[l+p+1][l+k-1]!="-1"){
                        res=mymax(res,dp[l+p+1][l+k-1]);
                    }
                }
            }
            dp[l][l+k]=res;
        }
    }
    if(dp[0][n]=="-1") cout<<"invalid"<<endl;
    else cout<<dp[0][n]<<endl;
    
    return 0;
}
