from collections import defaultdict def encode(key): k, u, r, o = key h = (k << 24) | (u << 16) | (r << 8) | o return h def decode(h): o = h & 0xff r = (h >> 8) & 0xff u = (h >> 16) & 0xff k = (h >> 24) & 0xff return k, u, r, o S = input() dp = defaultdict(int) dp[encode((0, 0, 0, 0))] = 0 for c in S: pp = dp.copy() dp, pp = pp, dp for key in pp.keys(): k, u, r, o = decode(key) nkey = None match c: case 'K': nkey = (k+1, u, r, o) case 'U': nkey = (k-1, u+1, r, o) case 'R': nkey = (k, u-1, r+1, o) case 'O': nkey = (k, u, r-1, o+1) case 'I': if o > 0: dp[encode((k, u, r, o-1))] = max(dp[encode((k, u, r, o-1))], pp[key] + 1) if nkey is not None and all(x >= 0 for x in nkey): dp[encode(nkey)] = max(dp[encode(nkey)], pp[key]) if c == '?': 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 nkey in nkeys: if all(x >= 0 for x in nkey): dp[encode(nkey)] = max(dp[encode(nkey)], pp[key]) # for 'I' if o > 0: dp[encode((k, u, r, o-1))] = max(dp[encode((k, u, r, o-1))], pp[key] + 1) ans = max(dp.values()) print(ans)