Example

Input

R?????,2?)


Output

29
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
string maxstr(string sx, string sy) {
	if (sx.size() != sy.size()) return (sx.size() > sy.size() ? sx : sy);
	return (sx > sy ? sx : sy);
}
map<string, bool> vis;
map<string, string> dp;
string solve(string s) {
	if (s == "0") return "0";
	if (s[0] == '0' || s[0] == '(' || s[0] == ')') return "invalid";
	if (vis[s]) return dp[s];
	int N = s.size();
	if (s.find('L') == string::npos && s.find('R') == string::npos && s.find('(') == string::npos && s.find(')') == string::npos && s.find(',') == string::npos) {
		for (int i = 0; i < N; ++i) {
			if (s[i] == '?') s[i] = '9';
		}
		return s;
	}
	if (N <= 5 || (s[0] != 'L' && s[0] != 'R' && s[0] != '?') || (s[1] != '(' && s[1] != '?') || (s[N - 1] != ')' && s[N - 1] != '?')) {
		return "invalid";
	}
	// Case of 'L'
	string ans;
	if (s[0] != 'R') {
		for (int i = 3; i < N - 2; ++i) {
			if (s[i] != ',' && s[i] != '?') continue;
			string lres = solve(s.substr(2, i - 2));
			string rres = solve(s.substr(i + 1, N - i - 2));
			if (lres != "invalid" && rres != "invalid") {
				ans = maxstr(ans, lres);
			}
		}
	}
	// Case of 'R'
	if (s[0] != 'L') {
		for (int i = 3; i < N - 2; ++i) {
			if (s[i] != ',' && s[i] != '?') continue;
			string lres = solve(s.substr(2, i - 2));
			string rres = solve(s.substr(i + 1, N - i - 2));
			if (lres != "invalid" && rres != "invalid") {
				ans = maxstr(ans, rres);
			}
		}
	}
	if (ans.empty()) ans = "invalid";
	vis[s] = true;
	dp[s] = ans;
	return ans;
}
int main() {
	string s;
	cin >> s;
	vis.clear();
	dp.clear();
	string ans = solve(s);
	cout << ans << endl;
	return 0;
}
