We define a regular brackets sequence by the following inductive rules:
For example, all of the following are regular brackets sequences:
(), [], (()), ()[], ()[()]
while none of the following are:
(, ], )(, ([)], ([(]
Given a brackets sequence a1a2…an, find the length of the longest regular brackets sequence that is a subsequence of it. That is, find the largest m for which there exist indices 1≤i1<i2<⋯<im≤n such that ai1ai2…aim is a regular brackets sequence.
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 1 and 100, inclusive.
The input ends with a line containing the single word end, which must not be processed.
For each test case, print on its own line the length of the longest regular brackets subsequence.
For the sequence ([([]])], one of the longest regular brackets subsequences is [([])], which has length 6.