#include <iostream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
map<string, int> arrname;
map<string, map< int, int> > mmp;
bool GetNum(string& temp, int nStart, int nEnd, int &num) {
stack<string> ns;
num = 0;
string name = "";
for (int i = nStart; i < nEnd; ++i) {
if (isdigit(temp[i])) num = num * 10 + (temp[i] - '0');
else if (isalpha(temp[i])) name += temp[i];
else if (temp[i] == '[') { if (arrname.count(name) == 0) return false; ns.push(name); name = ""; }
else if (temp[i] == ']') {
name = ns.top(); ns.pop();
if (mmp[name].count(num) == 0) return false;
num = mmp[name][num];
}
}
return true;
}
int Check(string& temp) {
int eq = temp.find('='), len = temp.size();
if (eq == string::npos) {
int cnt = 0;
string name = "";
name = temp[0];
for (int i = 2; temp[i] != ']'; i++) cnt = cnt * 10 + (temp[i] - '0');
if (cnt < 0 || arrname.count(name)) return 1;
arrname[name] = cnt;
}
else {
string leftname = "", rightname = "";
int leftNum = 0, rightNum = 0;
if(isalpha(temp[eq +1]))rightname = temp[eq + 1];
if (GetNum(temp, eq + 1, len, rightNum) == false) return 1;
leftname = temp[0];
if (arrname.count(leftname) == 0) return 1;
if(GetNum(temp, 2, eq - 1, leftNum) == false || leftNum < 0 || arrname[leftname] <= leftNum) return 1;
mmp[leftname][leftNum] = rightNum;
}
return 0;
}
int main()
{
int last = 0;
string temp = "";
while (true) {
int ret = 0,count = 1;
arrname.clear();
mmp.clear();
while (cin >> temp && temp[0] != '.') {
last = temp[0];
if (!ret && Check(temp)) ret = count;
++count;
}
if (last == '.') break;
cout << ret << endl;
last = temp[0];
}
return 0;
}