#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
char s[105];
int dp[105][105];
bool judge(int a,int b){return (s[a]=='['&&s[b]==']')||(s[a]=='('&&s[b]==')');}
int solve(int l,int r)
{
if(l>=r)return 0;
int &ret=dp[l][r];
if(ret!=-1)return ret;
ret=judge(l,r)?2+solve(l+1,r-1):0;
for(int i=l;i<r;++i)ret=max(ret,solve(l,i)+solve(i+1,r));
return ret;
}
int main()
{
while(~scanf("%s",s))
{
if(s[0]=='e')break;
memset(dp,-1,sizeof dp);
printf("%d
",solve(0,strlen(s)-1));
}
return 0;
}