#include<cstdio>#include<cstdlib>#include<iostream>#include<cstring>#include<string>#include<cmath>#include<ctime>#include<map>#include<sstream>#include<vector>#include<queue>#include<set>#include<algorithm>#include<stack>using namespace std;#define REP(x, y) for(int x = 0; x < y; x++)#define DWN(x, y) for(int x = y-1; x > -1; x--)#define LL __int64const int maxn = 110;int dp[maxn][maxn], n;char str[maxn];int dfs(int x, int y){ if(x > y) return 0; if(x == y) return 0; if(dp[x][y] > -1) return dp[x][y]; int &ans = dp[x][y]; ans = 0; if(str[x]=='('&&str[y]==')' || str[x]=='['&&str[y]==']') ans = dfs(x+1, y-1)+2; for(int k = x; k < y; k++) ans = max(ans, dfs(x, k)+dfs(k+1, y)); return ans;}int main(){ while(scanf("%s", str), str[0]!='e') { memset(dp, -1, sizeof(dp)); n = strlen(str); printf("%dn", dfs(0, n-1)); } return 0;}