Brackets

No attempts yetTime limit1sMemory limit128 MB

Problem

We define a regular brackets sequence by the following inductive rules:

  • the empty sequence is a regular brackets sequence;
  • if ss is a regular brackets sequence, then (s)(s) and [s][s] are regular brackets sequences;
  • if aa and bb are regular brackets sequences, then their concatenation abab is a regular brackets sequence;
  • no other sequence is a regular brackets sequence.

For example, all of the following are regular brackets sequences:

(), [], (()), ()[], ()[()]

while none of the following are:

(, ], )(, ([)], ([(]

Given a brackets sequence a1a2ana_1 a_2 \dots a_n, find the length of the longest regular brackets sequence that is a subsequence of it. That is, find the largest mm for which there exist indices 1i1<i2<<imn1 \le i_1 < i_2 < \dots < i_m \le n such that ai1ai2aima_{i_1} a_{i_2} \dots a_{i_m} is a regular brackets sequence.

Input

The input contains multiple test cases. Each test case is a single line consisting only of the characters (, ), [, and ]; every such line has length between 11 and 100100, inclusive.

The input ends with a line containing the single word end, which must not be processed.

Output

For each test case, print on its own line the length of the longest regular brackets subsequence.

Hint

For the sequence ([([]])], one of the longest regular brackets subsequences is [([])], which has length 66.