Example

Input

R?????,2?)


Output

29
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)

map< pair<int, int>, string> memo;

bool match(char c, string s)
{
	rep(i, 0, s.length()) if (s[i] == c) return true;
	return false;
}

string comp(string a, string b)
{
	if (a.length() < b.length())
	{
		return b;
	}
	else if (a.length() > b.length())
	{
		return a;
	}
	else
	{
		if (a < b)
			return b;
		else
			return a;
	}
}

string dfs(string s, int left, int right)
{
	auto p = make_pair(left, right);
	if (memo.find(p) != memo.end()) return memo[p];

	if(match(s[left], "(),")) return (memo[p] = "");
	if (s[left] == '0')
	{
		if (left == right)
			return (memo[p] = "0");
		else
			return (memo[p] = "");
	}

	bool ok = true;
	rep(i, left, right + 1)
	{
		if(!match(s[i], "0123456789?")) ok = false;
	}

	if (ok)
	{
		string ret = "";
		rep(i, left, right + 1)
		{
			if (s[i] == '?')
				ret += '9';
			else
				ret += s[i];
		}
		return (memo[p] = ret);
	}

	if (right - left + 1 < 6) return (memo[p] = "");

	if (!match(s[left], "?RL")) return (memo[p] = "");
	if (!match(s[left + 1], "?(")) return (memo[p] = "");
	if (!match(s[right], "?)")) return (memo[p] = "");

	string ret = "";
	rep(i, left + 3, right - 1) if (match(s[i], "?,"))
	{
		string l = dfs(s, left + 2, i - 1);
		string r = dfs(s, i + 1, right - 1);
		if (l.length() == 0 || r.length() == 0) continue;

		string cor;
		if (s[left] == 'R')
			cor = r;
		else if (s[left] == 'L')
			cor = l;
		else
			cor = comp(l, r);

		ret = comp(ret, cor);
	}

	return (memo[p] = ret);
}

int main()
{
	string s; cin >> s;
	string ans = dfs(s, 0, s.length() - 1);
	if (ans.length() == 0)
		cout << "invalid" << endl;
	else
		cout << ans << endl;
}