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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX/3

int n;
string s;

string maxs(string a,string b){
  if(a=="invalid")return b;
  if(b=="invalid")return a;
  if(a.length()==b.length()){
    return a>b?a:b;
  }else return a.length()>b.length()?a:b;
}

string dfs(int l,int r){
  if(l>=r)return "invalid";
  bool isnum=true;
  string res="invalid";
  repl(i,l,r){
    if(s[i]=='('||s[i]==')'||s[i]=='L'||s[i]=='R'||s[i]==',')isnum=false;
  }
  if(isnum){
    if(s[l]=='0'&&r-l>1)return "invalid";
    res="";
    repl(i,l,r){
      if(s[i]=='?')res+="9";
      else res+=s[i];
    }
  }else{
    if(r-l<4)return "invalid";
    if(s[l]!='L'&&s[l]!='R'&&s[l]!='?')return "invalid";
    if(s[l+1]!='('&&s[l+1]!='?')return "invalid";
    if(s[r-1]!=')'&&s[r-1]!='?')return "invalid";
    repl(i,l+3,r-2){
      if(s[i]!=','&&s[i]!='?')continue;
      string ln=dfs(l+2,i);
      string rn=dfs(i+1,r-1);
      if(ln!="invalid"&&rn!="invalid"){
        if(s[l]=='L')res=maxs(res,ln);
        else if(s[l]=='R')res=maxs(res,rn);
        else res=maxs(res,maxs(ln,rn));
      }
    }
  }
  return res;
}

int main(){
	cin.sync_with_stdio(false);
  cin>>s;
  n=s.length();
  cout<<dfs(0,n)<<endl;
	return 0;
}