# Jingle Balls

# Jingle Balls

It will soon be time to decorate the Christmas tree. The NWERC judges are already debating the optimal way to put decorations in a tree. They agree that it is essential to distribute the decorations evenly over the branches of the tree.

This problem is limited to binary Christmas trees. Such trees consist of a trunk, which splits into two subtrees. Each subtree may itself split further into two smaller subtrees and so on. A subtree that does not split any further is a twig. A twig may be decorated by attaching at most one ball to it.

** Figure 1** – Example of a tree with subtrees, twigs and one ball.

A decorated tree has an even distribution of balls if and only if the following requirement is satisfied:

At every point where a (sub)tree splits into two smaller subtrees **t _{1}** and

**t**, the total number of balls in the left subtree

_{2}**N(t**and the total number of balls in the right subtree

_{1})**N(t**must either be equal or differ by one. That is:

_{2})**|N(t1)-N(t2)|**≤

**1**.

In their enthusiasm, the judges initially attach balls to arbitrary twigs in the tree. When they can not find any more balls to put in the tree, they stand back and consider the result. In most cases, the distribution of the balls is not quite even. They decide to fix this by moving some of the balls to different twigs.

Given the structure of the tree and the initial locations of the balls, calculate the minimum number of balls that must be moved to achieve an even distribution as defined above.

Note that it is not allowed to add new balls to the tree or to permanently remove balls from the tree. The only way in which the tree may be changed is by moving balls to different twigs.

**Input**

For each test case, the input consists of one line describing a decorated tree.

The description of a tree consists of a recursive description of its subtrees. A (sub)tree is represented by a string in one of the following forms:

- The string '
**()**' represents a twig without a ball. - The string '
**(B)**' represents a twig with a ball attached to it. - The string '
**(t**' represents a (sub)tree that splits into the two smaller subtrees represented by_{1}t_{2})**t**and_{1}**t**, where_{2}**t**and_{1}**t**are strings in one of the forms listed here._{2}

A tree contains at least **2** and at most **1000** twigs.

**Output**

For each test case, print one line of output.

If it is possible to distribute the balls evenly through the tree, print the minimum number of balls that must be moved to satisfy the requirement of even distribution.

If it is not possible to distribute the balls evenly, print the word '**impossible**'.

((B)()) ((((B)(B))((B)()))(B)) (()(((B)(B))(B)))

0 impossible 1