#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; for (char c : S) { map pp = dp; // copy dp.clear(); for (auto &it : pp) { auto [k,u,r,o] = it.first; int val = it.second; Key nkey; bool has = true; auto push = [&](Key nk, int v){ if (get<0>(nk) >= 0 && get<1>(nk) >= 0 && get<2>(nk) >= 0 && get<3>(nk) >= 0) { dp[nk] = max(dp[nk], v); } }; // ---- match c: cases ---- if (c == 'K') { nkey = {k+1, u, r, o}; push(nkey, val); } else if (c == 'U') { nkey = {k-1, u+1, r, o}; push(nkey, val); } else if (c == 'R') { nkey = {k, u-1, r+1, o}; push(nkey, val); } else if (c == 'O') { nkey = {k, u, r-1, o+1}; push(nkey, val); } else if (c == 'I') { if (o > 0) { push({k, u, r, o-1}, val + 1); } } // ---- c == '?' の場合、4 方向全部試す + 'I' も試す ---- 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) { 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; }