#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
using namespace std;

struct Data{
	Data(int arg_index,int arg_value){
		index = arg_index;
		value = arg_value;
	}
	int index,value;
};

struct Info{
	Info(){
		size = 0;
	}

	void init(){
		size = 0;
		V.clear();
	}

	char name[80];
	int size;
	vector<Data> V;
};

Info info[1001];
int index = 0;


bool strCmp(char* base, char* comp){
	int length1 = 0,length2 = 0;
	for(int i=0;base[i] != '\0'; i++)length1++;
	for(int i=0;comp[i] != '\0'; i++)length2++;
	if(length1 != length2)return false;

	for(int i=0;base[i] != '\0'; i++){
		if(base[i] != comp[i])return false;
	}
	return true;
}

void strcpy(char* to,char* str){
	for(int i=0;str[i] != '\0';i++){
		to[i] = str[i];
		to[i+1] = '\0';
	}
}

int parse(char buf[81],int left,int right){

	int tmp,work_index,calc,calc_right,outer_index,address;
	char work[81];
	bool FLG = false;

	for(int a = left; a <= right; a++){
		if(buf[a] == '['){
			FLG = true;
			break;
		}
	}

	if(!FLG){
		tmp = 0;
		for(int a = left; a <= right; a++){
			tmp = 10*tmp + buf[a] - '0';
		}
		return tmp;
	}else{

		work_index = 0;
		for(calc = left; buf[calc] != '[';calc++){
			work[work_index++] = buf[calc];
		}
		work[work_index] = '\0';

		outer_index = -1;

		for(int i = 0; i < index; i++){
			if(strCmp(info[i].name,work)){
				outer_index = i;
				break;
			}
		}

		if(outer_index == -1){
			return -1;
		}else{
			for(calc_right = right;buf[calc_right] != ']';calc_right--);
			address = parse(buf,calc+1,calc_right-1);

			if(address == -1){
				return -1;
			}else{

				for(int k = 0; k < info[outer_index].V.size(); k++){
					if(address == info[outer_index].V[k].index){
						return info[outer_index].V[k].value;
					}
				}
				return -1;
			}
		}
	}
}

int main(){

	int line = 0,ans = 0,work_index,length,calc,tmp,outer_index,address,right_calc;
	int right_outer_index,right_address;
	char buf[81],pre = 'A',work[81];
	bool FLG,isExist,isNumOnly,isInV;


	while(true){
		scanf("%s",buf);

		if(buf[0] == '.'){
			if(pre == 'D')break;
			else{

				printf("%d\n",ans);

				for(int i = 0; i < index;i++){
					info[i].init();
				}
				pre = 'D';
				line = 0;
				index = 0;
				ans = 0;
				continue;
			}
		}else{
			pre = 'A';
			line++;
		}

		for(length = 0; buf[length] != '\0';length++);

		FLG = false;
		for(int i = length-1; i >= 0; i--){
			if(buf[i] == '='){
				FLG = true;
				break;
			}
		}

		if(!FLG){
			work_index = 0;
			for(calc = 0; buf[calc] != '[';calc++){
				work[work_index++] = buf[calc];
			}
			work[work_index] = '\0';

			isExist = false;

			for(int i = 0; i < index;i++){
				if(strCmp(work,info[i].name)){
					isExist = true;
					break;
				}
			}

			if(isExist){
				if(ans == 0){
					ans = line;
				}
			}else{

				for(right_calc = length-1; buf[right_calc] != ']';right_calc--);
				tmp = parse(buf,calc+1,right_calc-1);

				if(tmp == -1){
					if(ans == 0){
						ans = line;
					}
				}else{
					strcpy(info[index].name,work);
					info[index].size = tmp;
					index++;
				}
			}
		}else{

			outer_index = -1;
			work_index = 0;
			for(calc = 0; buf[calc] != '[';calc++){
				work[work_index++] = buf[calc];
			}
			work[work_index] = '\0';

			for(int i = 0; i < index; i++){
				if(strCmp(info[i].name,work)){
					outer_index = i;
					break;
				}
			}

			if(outer_index == -1){
				if(ans == 0){
					ans = line;
				}
			}else{

				for(right_calc = length-1;buf[right_calc] != '=';right_calc--);

				address = parse(buf,calc+1,right_calc-2);

				if(address == -1){
					if(ans == 0){
						ans = line;
					}
				}else{
					if(address < info[outer_index].size){

						isNumOnly = true;
						for(calc = right_calc+1;buf[calc] != '\0';calc++){
							if(buf[calc] == '['){
								isNumOnly = false;
								break;
							}
						}

						if(isNumOnly){
							tmp = 0;
							for(int k = right_calc+1;buf[k] != '\0'; k++){
								tmp = 10*tmp + buf[k] - '0';
							}

							isInV = false;
							for(int k = 0; k < info[outer_index].V.size();k++){
								if(info[outer_index].V[k].index == address){
									info[outer_index].V[k].value = tmp;
									isInV = true;
									break;
								}
							}
							if(!isInV){
								info[outer_index].V.push_back(Data(address,tmp));
							}
						}else{

							work_index = 0;
							for(calc = right_calc+1;buf[calc] != '[';calc++){
								work[work_index++] = buf[calc];
							}
							work[work_index] = '\0';

							right_outer_index = -1;

							for(int k = 0; k < index; k++){
								if(strCmp(info[k].name,work)){
									right_outer_index = k;
									break;
								}
							}

							if(right_outer_index == -1){
								if(ans == 0){
									ans = line;
								}
							}else{
								for(right_calc = length-1; buf[right_calc] != ']'; right_calc--);

								right_address = parse(buf,calc+1,right_calc-1);

								if(right_address == -1){
									if(ans == 0){
										ans = line;
									}
								}else{

									isInV = false;
									for(int k = 0; k < info[right_outer_index].V.size(); k++){
										if(right_address == info[right_outer_index].V[k].index){
											tmp = info[right_outer_index].V[k].value;
											isInV = true;
											break;
										}
									}

									if(!isInV){
										if(ans == 0){
											ans = line;
										}
									}else{

										isInV = false;
										for(int k = 0; k < info[outer_index].V.size();k++){
											if(address == info[outer_index].V[k].index){
												info[outer_index].V[k].value = tmp;
												isInV = true;
												break;
											}
										}

										if(!isInV){
											info[outer_index].V.push_back(Data(address,tmp));
										}
									}
								}
							}
						}
					}else{
						if(ans == 0){
							ans = line;
						}
					}
				}
			}
		}
	}

	return 0;
}

Measured memory_bytes: 2732