Example

Input

R?????,2?)


Output

29
#include <bits/stdc++.h>
using namespace std;
bool comp(const string& a, const string& b)
{
  if(a[0] == 'i') return(false);
  if(b[0] == 'i') return(true);
  if(a.size() != b.size()) return(a.size() > b.size());
  return(a > b);
}

string S;
string dp[50][51];

string tonum(int left, int right)
{
  string ss;
  if(S[left] == '0' && left + 1 < right) return("invalid");
  while(left < right) {
    if(!isdigit(S[left]) && S[left] != '?') return("invalid");
    if(!isdigit(S[left])) ss += "9";
    else ss += S[left];
    ++left;
  }
  return(ss);
}
string rec(int left, int right)
{
  
  if(dp[left][right] != "*") return(dp[left][right]);
  string ret = tonum(left, right);
  if(right - left >= 6) {
    if((S[left + 1] == '?' || S[left + 1] == '(') && (S[right - 1] == '?' || S[right - 1] == ')')) {
      for(int i = left + 3; i + 3 <= right; i++) {
        if(S[i] == '?' || S[i] == ',') {
          string L = rec(left + 2, i), R = rec(i + 1, right - 1);
          if(L[0] == 'i' || R[0] == 'i') continue;
          if(S[left] == 'L' || S[left] == '?') ret = min(ret, L, comp);
          if(S[left] == 'R' || S[left] == '?') ret = min(ret, R, comp);
        }
      }
    }
  }
  return(dp[left][right] = ret);
}

int main()
{
  fill_n(*dp, 50 * 51, "*");
  cin >> S;
  cout << rec(0, S.size()) << endl;
}