#include <bits/stdc++.h>
using namespace std;

using State = string::const_iterator;

void declaration(State &itr);
void assingnment(State &itr);
int expr(State &itr);
int number(State &itr);
string array_name(State &itr);

int line;
bool error;
map<string, map<int,int> > table;
map<string, int> arr_size;

void Error(State &itr){
    error = true;
    while(*itr){
        itr++;
    }
}

void consume(State &itr, char expected){
    if(*itr == expected){
        itr++;
    }else{  // error
        // cout << "expected " << expected << " but " << *itr << endl;
        Error(itr);
    }
}

bool valid(string name, int idx){
    return arr_size.find(name)!=arr_size.end() && idx>=0 && idx<arr_size[name];
}

void declaration(State &itr){
    string name = array_name(itr);
    consume(itr, '[');
    int size = number(itr);
    consume(itr, ']');
    if(!error){
        arr_size[name] = size;
    }
    // cout << name << " " << size << endl;
}

void assingnment(State &itr){
    string name = array_name(itr);
    consume(itr, '[');
    int idx = expr(itr);
    consume(itr, ']');
    consume(itr, '=');
    int val = expr(itr);
    if(valid(name, idx)){
        table[name][idx] = val;
    }else{
        Error(itr);
    }
    // cout << name << " " << idx << " " << val << endl;
}

int expr(State &itr){
    int val = -1;
    if(isdigit(*itr)){
        val = number(itr);
    }else if(isalpha(*itr)){
        string name = array_name(itr);
        consume(itr, '[');
        int idx = expr(itr);
        consume(itr, ']');
        if(valid(name, idx) && table[name].find(idx) != table[name].end()){
            val = table[name][idx];
        }else{ // error
            // cout << "invalid name or index" << endl;
            Error(itr);
        }
    }else{ // error
        // cout << "invalid char" << endl;
        Error(itr);
    }
    if(val == -1){
        Error(itr);
    }
    return val;
}

int number(State &itr){
    string digits = "";
    while(isdigit(*itr)){
        digits.push_back(*itr);
        itr++;
    }
    if(digits.empty() || (digits.size()>1 && *digits.begin()=='0')){  // error
        // cout << "this is not number" << endl;
        Error(itr);
        return 0;
    }
    return stoi(digits);
}

string array_name(State &itr){
    string name = "";
    while(isalpha(*itr)){
        name.push_back(*itr);
        itr++;
    }
    if(name.empty()){ // error
        // cout << "this is not name" << endl;
        Error(itr);
        return "";
    }
    return name;
}

int solve(){
    string program;
    getline(cin, program);
    if(program == ".") return 1;
    line = 1;
    error = false;
    table.clear();
    arr_size.clear();
    while(program != "."){
        if(!error){
            State begin = program.begin();
            if(program.find('=') != program.npos){
                assingnment(begin);
            }else{
                declaration(begin);
            }
            if(error){
                cout << line << endl;
            }
            line++;
        }
        getline(cin, program);
    }
    if(!error){
        cout << 0 << endl;
    }
    return 0;
}

int main(){
    while(solve()==0);
    return 0;
}

