Twenty Four, Again

Given four numbers in fixed order, find the minimum grade (parentheses plus adjacent-swap inversions) of an expression equal to 24 using each number once, with integer-only division.

Medium7Brute forceBacktrackingMathImplementationNo attempts yetTime limit2sMemory limit512 MB

Problem

Four base values are given in a fixed order. Using all four values, the four arithmetic operations, and parentheses where you need them, you build an expression whose value is 24. With the base values 3 5 5 2 there are many ways to do it, among them 5*5-3+2 and (3+5)*(5-2). Multiplication and division bind tighter than addition and subtraction, and operators of equal precedence are evaluated from left to right.

An expression also gets a grade. A perfect grade is 0. Each pair of parentheses adds one point, and each inversion, meaning one swap of two adjacent values in the original ordering, adds two points. The number of inversions is the smallest number of adjacent swaps that turns the original ordering into the one you used. The first expression above has grade 4, since two swaps are needed to move the 3 into third position. The second has grade 2, since it keeps the original ordering and uses two pairs of parentheses. With the base values 3 6 2 3 the expression (3+6+3)*2 has grade 3, while 3*6+2*3 has a perfect grade 0. The lower the grade, the better the expression.

Two more rules apply. Unary minus is not allowed, so the base values 3 5 5 2 cannot produce -3+5*5+2. Division may be used only when that division comes out as an integer, so the base values 2 3 4 9 cannot produce 2/3*4*9. Each of the four base values is used exactly once.

Given the base values, find the lowest grade of an expression whose value is 24.

Input

The input is a single line with four base values separated by spaces. Every base value is an integer between 1 and 100, inclusive.

Output

Print the lowest grade reachable with the given base values. If 24 cannot be produced, print impossible.