#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
#include <map>
using namespace std;

int def['z'];
typedef pair<char, int> VAR;
map<VAR, int> list;
string s;


typedef string::const_iterator State;
class ParseError {};
class NotDefined {};

// beginがexpectedを指していたらbeginを一つ進める。
void consume(State &begin, char expected) {
	if (*begin == expected) {
		begin++;
	} else {
		cerr << "Expected '" << expected << "' but got '" << *begin << "'" << endl;
		cerr << "Rest string is '";
		while (*begin) {
			cerr << *begin++;
		}
		cerr << "'" << endl;
		throw ParseError();
	}
}

int number(State &begin) {
	int ret = 0;
	while (isdigit(*begin)) ret = ret*10 + *begin++ - '0';
	return ret;
}

int expression(State &begin) {
	if(isdigit(*begin)) return number(begin);
	VAR ret;
	ret.first = *begin++;
	consume(begin, '[');
	ret.second = expression(begin);
	consume(begin, ']');
	if(def[ret.first] <= ret.second) throw NotDefined();
	if(list.find(ret) == list.end()) throw NotDefined();
	return list[ret];
}

void assignment(State &begin){
	VAR ret;
	ret.first = *begin++;
	consume(begin, '[');
	ret.second = expression(begin);
	consume(begin, ']');
	if(def[ret.first] <= ret.second) throw NotDefined();
	consume(begin, '=');
	int ret2 = expression(begin);
	list[ret] = ret2;
}

void declaration(State &begin){
	char name = *begin++;
	consume(begin, '[');
	int ret = number(begin);
	consume(begin, ']');
	def[name] = ret;
}

void program(State &begin){
	if(s.find('=') != string::npos) assignment(begin);
	else declaration(begin);
}

int main(void) {
	while(getline(cin, s), s != ".") {
		int l = 1, f = 0;
		list.clear();
		memset(def, 0, sizeof(def));
		do{
			if(f) continue;
			State begin = s.begin();
			try{
				program(begin);
			}catch(NotDefined a){
				cout << l << endl;
				f = 1;
			}
			l ++;
		}while(getline(cin, s), s != ".");
		if(!f) cout << "0" << endl;
	}
	return 0;
}