import sys # sys.setrecursionlimit(200005) # sys.set_int_max_str_digits(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = -1-(-1 << 31) # inf = -1-(-1 << 63) # md = 10**9+7 md = 998244353 from collections import defaultdict def pack(arr): s = 0 for a in arr: s = s*21+a return s def unpack(p): res = [] for _ in range(4): p, r = divmod(p, 21) res.append(r) return res[::-1] def update(c): kuro = pkuro[:] val = pre[p] if c == 0 and kuro[0] < 20: kuro[0] += 1 elif c == 4: if kuro[c-1]: kuro[c-1] -= 1 val += 1 elif kuro[c-1] and kuro[c] < 20: kuro[c-1] -= 1 kuro[c] += 1 q = pack(kuro) if val > dp[q]: dp[q] = val s = SI() dp = defaultdict(int) dp[0] = 0 for c in s: c = "KUROI?".find(c) if c == -1: continue pre, dp = dp, defaultdict(int) for p in pre: pkuro = unpack(p) if c == 5: for i in range(5): update(i) else: update(c) # pDB(pre, dp) print(max(dp.values()))