#include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); string S; cin >> S; using Key = tuple; map dp; dp[{0,0,0,0}] = 0; auto in_range = [&](const Key& t){ return 0 <= get<0>(t) && get<0>(t) <= 20 && 0 <= get<1>(t) && get<1>(t) <= 20 && 0 <= get<2>(t) && get<2>(t) <= 20 && 0 <= get<3>(t) && get<3>(t) <= 20; }; for (char c : S) { map pp = dp; // pp = dp.copy() dp = pp; // dp, pp = pp, dp ←「何もしない」遷移も維持 for (auto const& it : pp) { const Key& key = it.first; auto [k,u,r,o] = key; int val = it.second; auto push = [&](Key nk, int v){ if (in_range(nk)) { dp[nk] = max(dp[nk], v); } }; Key nkey; bool has_nkey = true; // --- match c --- if (c == 'K') { nkey = {k+1, u, r, o}; } else if (c == 'U') { nkey = {k-1, u+1, r, o}; } else if (c == 'R') { nkey = {k, u-1, r+1, o}; } else if (c == 'O') { nkey = {k, u, r-1, o+1}; } else if (c == 'I') { has_nkey = false; if (o > 0) { push({k, u, r, o-1}, val + 1); } } else { has_nkey = false; } // normal transitions (non-I) if (has_nkey) { if (in_range(nkey)) { push(nkey, val); } } // --- '?' case --- if (c == '?') { vector nkeys = { {k+1, u, r, o}, {k-1, u+1, r, o}, {k, u-1, r+1, o}, {k, u, r-1, o+1}, }; for (auto &nk : nkeys) { if (in_range(nk)) push(nk, val); } if (o > 0) { push({k, u, r, o-1}, val + 1); } } } } int ans = 0; for (auto &it : dp) ans = max(ans, it.second); cout << ans << "\n"; return 0; }