#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <queue>
#include <cmath>
#include <algorithm>
#include <functional>
#include <cctype>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
int toint(char c){
	if('a'<=c&&c<='z') return c-'a';
	else return c-'A'+26;
}
int sz[52];
map<int,int> val[52];
string s;
int ex(int& be){
	if(isdigit(s[be])){
		int num=0;
		while(isdigit(s[be])){
			num*=10;
			num+=s[be]-'0';
			be++;
		}
		return num;
	}
	char c=toint(s[be]);
	be+=2;
	if(isdigit(s[be])){
		int num=0;
		while(isdigit(s[be])){
			num*=10;
			num+=s[be]-'0';
			be++;
		}
		if(sz[c]<=num) return -1;
		if(val[c].count(num)==0) return -1;
		return val[c][num];
	}else{
		int x=ex(be);
		if(x==-1) return -1;
		if(sz[c]<=x) return -1;
		if(val[c].count(x)==0) return -1;
		return val[c][x];
	}
}
int main(){
	while(true){
		rep(i,52) sz[i]=0;
		rep(i,52) val[i].clear();
		int bug=0;
		int tt;
		for(tt=0;;tt++){
			cin>>s;
			if(s[0]=='.') break;
			bool def=true;
			rep(i,s.size()) if(s[i]=='=') def=false;
			if(def){
				int now=2,num=0;
				while(isdigit(s[now])){
					num*=10;
					num+=s[now]-'0';
					now++;
				}
				sz[toint(s[0])]=num;
//				cout<<s[0]<<" "<<num<<endl;
			}else{
				int c=toint(s[0]);
				int be=2;
				int x=ex(be);
				while(s[be]!='=') be++;
				be++;
				int y=ex(be);
				if(x==-1||sz[c]<=x||y==-1){
//					show(x);
//					show(y);
					bug=tt+1;
//					puts("bughere");
					string ss;
					while(true){
						cin>>ss;
						if(ss[0]=='.') break;
					}
					break;
				}
				val[c][x]=y;
			}
		}
		if(tt==0) break;
		cout<<bug<<endl;
	}
}