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

string S[1009]; int n, sz[52]; map<int, int>M[52]; int c[128]; bool flag = false;

int number(string U, int L, int R) {
	if (U[L] >= '0' && U[L] <= '9') return stoi(U.substr(L, R - L + 1));
	int G = number(U, L + 2, R - 1);
	int ID = c[(int)(U[L])];
	if (G == -1 || G >= sz[ID]) return -1;
	return M[ID][G] - 1;
}

int position(string U) {
	return number(U.substr(2, U.size() - 3), 0, U.size() - 4);
}

bool bugged(int ID, int L, int R) {
	string T = S[ID]; bool I = false;
	for (int i = 0; i < T.size(); i++) {
		if (T[i] == '=')I = true;
	}
	if (I == true) {
		int pos = 0;
		for (int i = 0; i < T.size(); i++) { if (T[i] == '=')pos = i; }
		string F1 = T.substr(0, pos);
		string F2 = T.substr(pos + 1, T.size() - (pos + 1));
		int P1 = position(F1);
		int P2 = number(F2, 0, F2.size() - 1);
		int ID = c[(int)(F1[0])];
		if (P1 == -1 || P1 >= sz[ID] || P2 == -1) return true;
		M[ID][P1] = P2 + 1;
		return false;
	}
	else {
		char C = T[0];
		int size_ = stoi(T.substr(2, T.size() - 3));
		sz[c[(int)C]] = size_;
		return false;
	}
	return true;
}

int main() {
	int cnte = 0;
	for (char i = 'A'; i <= 'Z'; i++) { c[(int)i] = cnte; cnte++; }
	for (char i = 'a'; i <= 'z'; i++) { c[(int)i] = cnte; cnte++; }
	while (true) {
		n = 0; flag = false;
		while (true) {
			cin >> S[n]; if (S[n] == ".") { break; }
			n++;
		}
		if (n == 0) break;
		for (int i = 0; i < 52; i++) { M[i].clear(); sz[i] = -1; }
		for (int i = 0; i < n; i++) {
			if (bugged(i, 0, S[i].size() - 1) == true) { cout << i + 1 << endl; flag = true; break; }
		}
		if (flag == false) { cout << "0" << endl; }
	}
	return 0;
}
