Example

Input

R?????,2?)


Output

29
#include <string>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
class Parser{
public:
  string s;
  vector< vector<string> > DP;
  string parse(){
    int n = s.length();
    DP = vector< vector<string> >(n+1, vector<string>(n+1,"?"));
    return expr(0,s.length());
  }

  string expr(int l, int r){
    if(DP[l][r] != "?") return DP[l][r];
    string ret = "!";
    ret = comp(ret, L(l,r));
    ret = comp(ret, R(l,r));
    ret = comp(ret, num(l,r));
    //printf("expr(%d,%d) = ",l,r);cout << ret << endl;
    DP[l][r] = ret;
    return ret;
  }
  
  string num(int l, int r){
    string ret;
    if(r - l > 1 && s[l] == '0') return "!";
    for(int i = l; i < r; ++i){
      if(isdigit(s[i])) ret += s[i];
      else if(s[i] == '?') ret += '9';
      else return "!";
    }
    return ret;
  }

  string comp(const string &a, const string &b){
    if(a == "!" && b != "!") return b;
    if(b == "!" && a != "!") return a;
    if(a.length() > b.length()) return a;
    if(a.length() < b.length()) return b;
    for(int i = 0; i < (int)a.length(); ++i){
      if(a[i] < b[i]) return b;
      if(a[i] > b[i]) return a;
    }
    return a;
  }

  string L(int l, int r){
    if(s[l] != '?' && s[l] != 'L') return "!";
    if(s[l+1] != '(' && s[l+1] != '?') return "!";
    if(s[r-1] != ')' && s[r-1] != '?') return "!";
    string ret = "!";
    for(int i = l+3; i < r-2; ++i){
      if(s[i] != ',' && s[i] != '?') continue;
      string x = expr(l+2,i), y = expr(i+1,r-1);
      if(y == "!" || x == "!") continue;
      ret = comp(ret,x);
    }
    return ret;
  }

  string R(int l, int r){
    if(s[l] != '?' && s[l] != 'R') return "!";
    if(s[l+1] != '(' && s[l+1] != '?') return "!";
    if(s[r-1] != ')' && s[r-1] != '?') return "!";
    string ret = "!";
    for(int i = l+3; i < r-2; ++i){
      if(s[i] != ',' && s[i] != '?') continue;
      string x = expr(l+2,i), y = expr(i+1,r-1);
      if(y == "!" || x == "!") continue;
      ret = comp(ret,y);
    }
    return ret;
  }
};

int main(){
  string s;
  cin >> s;
  Parser p;
  p.s = s;
  string ans = p.parse();
  if(ans == "!") cout << "invalid" << endl;
  else cout << ans << endl;
  return 0;
}

