#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>
#include <tuple>
#include <array>
#include <climits>
#include <bitset>
#include <cassert>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define UNIQUE(v) (v).erase(unique(ALL(v)), (v).end())
#define MP make_pair
#define MT make_tuple

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;

struct parser{
	vector<int> sz;
	vector<map<int, int>> var;
	vector<string> prog;

	parser(vector<string> &vs){
		prog = vs;
		sz.resize(256);
		var.resize(256);
	}

	int parse(){
		for (int i = 0; i < prog.size(); ++i){
			if (!program(prog[i])) return i + 1;
		}
		return 0;
	}

	int program(const string &s){
		if (s.find("=") == -1) return declaration(s);
		return assignment(s);
	}

	bool declaration(const string &s){
		int len = s.size();
		char ar = s[0];
		sz[ar] = number(s.substr(2, len - 3));
		return true;
	}

	bool assignment(const string &s){
		int eq = 0;
		while (s[eq] != '=') ++eq;

		char ar = s[0];
		int idx = expression(s.substr(2, eq - 3));
		if (idx < 0 || idx >= sz[ar]) return false;
		int asgn = expression(s.substr(eq + 1));
		if (asgn < 0) return false;
		var[ar][idx] = asgn;
		return true;
	}

	int expression(const string &s){
		if (!isalpha(s[0])) return number(s);
		int len = s.size();
		char ar = s[0];
		int idx = expression(s.substr(2, len - 3));
		if (idx < 0 || idx >= sz[ar]) return -1;
		if (!var[ar].count(idx)) return -1;
		return var[ar][idx];
	}

	int number(const string &s){
		return stoi(s);
	}

};

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout.setf(ios::fixed);
	cout.precision(20);

	while (1){
		vector<string> vs;
		string s;
		while (cin >> s, s != "."){
			vs.emplace_back(s);
		}
		if (vs.empty()) break;
		parser p(vs);
		cout << p.parse() << endl;
	}

}