#include<string>
#include<sstream>
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cstdio>
//#include<bits/stdc++.h>
using namespace std;

vector<string> code;
map<string, int> val;
map<char, int> a;

int get_val(string str) {
	if(str.find("[") == string::npos) {
		stringstream ss(str);
		int v; ss >> v;
		return v;
	}
	if(count(str.begin(), str.end(), '[') == 1) {
		if(!val.count(str)) return -1;
		return val[str];
	}
	string cur = str.substr(str.find("[") + 1);
	int v = get_val(cur);
	stringstream ss;
	ss << v;
	string s; ss >> s;
	if(!val.count(str.substr(0, 2) + s)) return -1;
	return val[str.substr(0, 2) + s];
}
void add_arr(const string str) {
	stringstream ss(str.substr(2));
	int v; ss >> v;
	a[str[0]] = v;
}
int main() {
	ios::sync_with_stdio(false);
	string line, l, r, name;
	int rval, lval, bug;
	while(code.clear(), val.clear(), a.clear(), bug = -1, true) {
		cin >> line;
		if(line == ".") break;
		while(true) {
			code.push_back(line);
			cin >> line;
			if(line == ".") break;
		}
		for(size_t i = 0; i != code.size(); i++) {
			if(code[i].find("=") == string::npos) 
				add_arr(code[i]);
			else {
				l = code[i].substr(0, code[i].find("="));
				r = code[i].substr(code[i].find("=") + 1);
				rval = get_val(r.substr(0, r.find("]")));
				lval = get_val(l.substr(l.find("[") + 1, l.find("]") - l.find("[") - 1));
				if(rval == -1 || lval == -1 || !a[code[i][0]] || lval >= a[code[i][0]]) {bug = i; break;}
				stringstream ss;
				ss << lval;
				string s; ss >> s;
				name = code[i].substr(0, 2) + s;
				val[name] = rval;
			}
		}
		cout << bug + 1 << endl;
	}
	return 0;
}