#include <stdio.h>
#include <string.h>
#include <limits.h>

#define ERROR INT_MAX

int maxint(a, b) {
	return (a>b)?a:b;
}

struct pack {
	char *s;
	int depth;
};

struct pack in(char *s, int depth, int max);

struct pack out(char *s, int depth, int max) {
	struct pack tmp;
	if((*s)=='\0') {
		tmp.s=NULL;tmp.depth=maxint(max, depth);
		return tmp;
	}
	if(strchr(")]}", *s)!=NULL) {
		tmp.s=NULL;tmp.depth=ERROR;
		return tmp;
	}
	if(strchr("({[", *s)!=NULL) {
		tmp = in(s+1, depth+1, max);
		return out(tmp.s, depth, maxint(max, tmp.depth));
	}
	return out(s+1, depth, max);
}

struct pack in(char *s, int depth, int max) {
	struct pack tmp;
	if((*s)=='\0') {
		tmp.s=s;tmp.depth=ERROR;
		return tmp;
	}
	if(strchr(")]}", *s)!=NULL) {
		tmp.s=s+1;tmp.depth=maxint(max, depth);
		return tmp;
	}
	if(strchr("([{", *s)!=NULL) {
		struct pack tmp = in(s+1, depth+1, max);
		return in(tmp.s, depth, maxint(max, tmp.depth));
	}
	return in(s+1, depth, max);
}

int main(void) {
	char a[256];
	struct pack tmp;
	int i = 4;
	while(i--) {
		fgets(a, 256, stdin);
		tmp = out(a, 0, 0);
		printf("%d\n", tmp.depth);
	}
	return 0;
}

