#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <complex>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <iterator>

using namespace std;

#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define iter(c) __typeof__((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;

const int INFTY=1<<29;
const double EPS=1e-9;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
	os<<'[';
	rep(i,a.size()) os<<(i?" ":"")<<a[i];
	return os<<']';
}

pair<pair<char,int>,int> variable(const string& s,int i,map<char,int>& ntos,map<pair<char,int>,int>& vton);

pair<int,int> expression(const string& s,int i,map<char,int>& ntos,map<pair<char,int>,int>& vton)
{
	if(isdigit(s[i])){
		int x=0;
		for(;i<s.size() && isdigit(s[i]);i++)
			x=x*10+s[i]-'0';
		return mp(x,i);
	}
	else{
		auto p=variable(s,i,ntos,vton);
		char name; int index;
		tie(name,index)=p.first;
		if(index>=ntos[name]) throw "out of range";
		if(!vton.count(mp(name,index))) throw "undefined";
		return mp(vton[p.first],p.second);
	}
}

pair<pair<char,int>,int> variable(const string& s,int i,map<char,int>& ntos,map<pair<char,int>,int>& vton)
{
	char name=s[i];
	auto p=expression(s,i+2,ntos,vton);
	return mp(mp(name,p.first),p.second+1);
}

void program(const string& s,map<char,int>& ntos,map<pair<char,int>,int>& vton)
{
	if(s.find('=')==string::npos){ // declaration
		ntos[s[0]]=atoi(s.substr(2,s.size()-3).c_str());
	}
	else{ // assignment
		auto p=variable(s,0,ntos,vton);
		char name; int index;
		tie(name,index)=p.first;
		if(index>=ntos[name]) throw "out of range";
		assert(s[p.second]=='=');
		auto q=expression(s,p.second+1,ntos,vton);
		vton[p.first]=q.first;
	}
}

int main()
{
	for(string s;getline(cin,s) && s!=".";){
		vs lines;
		do{
			lines.push_back(s);
			getline(cin,s);
		}while(s!=".");
		
		//dump(lines);
		
		map<char,int> ntos; // name -> size
		map<pair<char,int>,int> vton; // variable -> number
		
		int res=0;
		rep(i,lines.size()){
			try{
				program(lines[i],ntos,vton);
				//cout<<"ntos:"; for(auto p:ntos) cout<<' '<<p; cout<<endl;
				//cout<<"vton:"; for(auto p:vton) cout<<' '<<p; cout<<endl;
			}
			catch(...){
				res=i+1;
				break;
			}
		}
		cout<<res<<endl;
	}
}