Combinator Expression

No attempts yetTime limit1sMemory limit256 MB

Problem

Combinatory logic is a computation model that expresses any computable function as a composition of functions from a small fixed basis. This problem uses BCKI, a restricted form of the BCKW basis.

A combinator expression over BCKI is a string produced by the grammar below.

<Expression> ::= <Expression> <Term> | <Term>
<Term>       ::= '(' <Expression> ')' | 'B' | 'C' | 'K' | 'I'

An expression is a tree of applications whose leaves are the combinators BB, CC, KK and II. Application is left associative, so BICBIC is the same as (BI)C(BI)C and is not the same as B(IC)B(IC).

In the explanations below, lowercase English letters a to z stand for sub-expressions. They never appear in the input. For example, BICBIC matches the shapes BxCBxC (with x=Ix = I), xx (with x=BICx = BIC), xyxy (with x=BIx = BI and y=Cy = C) and BxyBxy (with x=Ix = I and y=Cy = C), but it does not match BxBx.

In the expression pqpq we say that pp is applied to qq. You can read pp as a function and qq as its argument. The evaluation is not the usual passing of values through a fixed tree. Each step rewrites the tree, and the result is again a combinator expression.

One step picks a sub-expression that matches one of the patterns in the table, meaning there are sub-expressions xx (and possibly yy and zz) that make the pattern equal to the chosen sub-expression. The step then replaces that sub-expression with the reduction result.

PatternReduction resultName
BxyzBxyzx(yz)x(yz)composition function
CxyzCxyz(xz)y(xz)yexchange function
KxyKxyxxconstant function
IxIxxxidentity function

Steps repeat until no sub-expression matches any pattern. The expression that remains is the normal form of the original one.

Take CIC(CB)ICIC(CB)I, which reads as (((CI)C)(CB))I(((CI)C)(CB))I. With x=Ix = I, y=Cy = C and z=CBz = CB, the sub-expression (((CI)C)(CB))(((CI)C)(CB)) equals CxyzCxyz, so it becomes (xz)y=I(CB)C(xz)y = I(CB)C and the whole expression becomes I(CB)CII(CB)CI.

Now take B((CK)I)ICB((CK)I)IC. Reducing BB with x=(CK)Ix = (CK)I, y=Iy = I and z=Cz = C gives ((CK)I)(IC)((CK)I)(IC). Reducing the inner II gives ((CK)I)C((CK)I)C. Reducing CC gives (KC)I(KC)I, and reducing KK leaves CC.

The normal form does not depend on the order of the steps, but the number of steps does. In C(K(II)(IC))C(K(II)(IC)), reducing the inner ICIC first gives C(K(II)C)C(K(II)C), then IIII gives C((KI)C)C((KI)C), then KK gives CICI, which is three steps. Reducing IIII first gives C((KI)(IC))C((KI)(IC)), and then KK throws (IC)(IC) away and gives CICI in two steps.

Write a program that finds the smallest number of reduction steps that take a given combinator expression to its normal form.

Input

The first line contains a combinator expression that follows the grammar above. Its length is at most 30000. The line has no whitespace and no character outside the grammar.

Output

Print one integer, the smallest number of reduction steps needed to bring the given expression to its normal form.