#include <vector>
#include <map>
#include<algorithm>
#include<cmath>
#include<set>
#include<cstdio>
#include <sstream>
#include <string>
#include<iostream>
#include<iomanip>
#include<stack>
#include<utility>
#include<unordered_map>
#include<queue>
#include<cstring>
#include <list>
#include<deque>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
unordered_map<string, int >var;
unordered_map<string, map<int, int>>isassigened;
int eval(const string &s)
{
if (s.find("[") == -1)
{
int value= (int)stoi(s);
return value;
}
string variable = s.substr(0, s.find("["));
string subs = s.substr(s.find("[") + 1, s.rfind("]") - s.find("[") - 1);
int ret = eval(subs);
if (ret == -1 || var.find(variable) == var.end() || var[variable] <= ret || isassigened[variable].find(ret) == isassigened[variable].end())
return -1;
else
return isassigened[variable][ret];
}
int main()
{
//freopen("input.txt", "r", stdin);
string statement;
while (cin >> statement && statement != ".")
{
isassigened.clear();
var.clear();
int bugline = 0;
bool bug = 0;
do
{
if (bug == 0)
{
if (statement.find("=") == -1)
{
string name = statement.substr(0, statement.find("["));
string s = statement.substr(statement.find("[") + 1, statement.find("]") - statement.find("[") - 1);
int ints = (int)stoi(s);
var[name] = ints;
}
else
{
int pos = statement.find("=");
string left = statement.substr(0, pos);
string left1=left.substr(left.find("[") + 1, left.rfind("]") - left.find("[") - 1);
string right = statement.substr(pos+1, statement.size() - pos - 1);
int leftret = eval(left1);
int rightret = eval(right);
if (leftret != -1 && rightret != -1)
{
string name = left.substr(0, left.find("["));
if (leftret < var[name])
{
isassigened[name][leftret] = rightret;
}
else
bug = 1;
}
else
bug = 1;
}
++bugline;
}
} while (cin >> statement && statement != ".");
if (bug == 0)
cout << 0;
else
cout << bugline;
cout << endl;
}
}