#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <numeric>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <functional>
#include <complex>
#include <stack>
#include <tuple>
#include <array>
#include <ctype.h> // isdigit
using namespace std;

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define ALL(u) begin(u),end(u)
#define PB push_back
#define LE(n,m) ((n) < (m) + EPS)
#define GE(n,m) ((n) + EPS > (m))
#define EQ(n,m) (abs((n)-(m)) < EPS)

class Array{
public:
    int size;
    map<int, int> values;
};

typedef long long int ll;
const int INF = (1<<30) - 1;
const double EPS = 1e-9;
const int MOD = 1000000007;

int number(string s, int &pos){
    int x = 0;
    while(isdigit(s[pos])){
        x = x*10 + s[pos] - '0';
        pos++;
    }
    return x;
}

string array_name(string s, int &pos){
    int prev = pos;
    while(('a'<=s[pos]&&s[pos]<='z') || ('A'<=s[pos]&&s[pos]<='Z'))
        pos++;
    return s.substr(prev, pos-prev);
}

void declaration(string s, int &pos, map<string, Array> &decs){
    string name = array_name(s, pos);
    
    pos++;
    int num = number(s, pos);
    pos++;
    
    Array array = Array();
    array.size = num;
    decs[name] = array;
}

int expression(string s, int &pos, map<string, Array> &decs){
    if(isdigit(s[pos])){
        //number
        return number(s, pos);
    }else{
        //array
        //?????°??£?????????-1?????????
        string name = array_name(s, pos);
        pos++;
        int num = expression(s, pos, decs);
        pos++;
        
        if(decs.find(name) == decs.end())
            return -1;
        if(decs[name].size <= num)
            return -1;
        if(decs[name].values.find(num) == decs[name].values.end())
            return -1;
        
        return decs[name].values[num];
    }
    //NotInplemtend
    return -2;
}

bool assignment(string s, int &pos, map<string, Array> &decs){
    //?????°??£?????????false
    string name = array_name(s, pos);
    pos++;
    int num = expression(s, pos, decs);
    pos++;
    
    if(decs.find(name) == decs.end())
        return false;
    if(decs[name].size <= num)
        return false;
    
    pos++;
    int num2 = expression(s, pos, decs);
    if(num2 == -1)
        return false;
    
    decs[name].values[num] = num2;
    
    return true;
}


int solve(vector<string> &program){
    map<string, Array> decs;
    for(int i=0; i<program.size(); i++){
        string &s = program[i];
        if(s.find("=") == string::npos){
            //??????
            int pos = 0;
            declaration(s, pos, decs);
        }else{
            //??£??\
            int pos = 0;
            if(!assignment(s, pos, decs)){
                return i+1;
            }
        }
    }
    return 0;
}

//g++ -std=c++0x -msse4.2 -O3
//#include <bits/stdc++.h>
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    //cout.precision(16);
    //cout.setf(ios::fixed);
    
    while(true){
        vector<string> program;
        while(true){
            string s; cin >> s;
            if(s == "."){
                if(program.size() == 0)
                    return 0;
                
                cout << solve(program) << endl;
                break;
            }
            program.push_back(s);
        }
    }
    
    return 0;
}

Measured memory_bytes: 3212