#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <map>
#include <algorithm>
using namespace std;
#define INF 0x7FFFFFFF
#define ll long long

int get_num();

string s;
vector<map<int,int> > a;
vector<int> b;
vector<int> c;
int t;


void pr(vector<int> k){
  cout <<  "x ";
  for(int i=0;i<k.size();i++){
    cout << k[i] << " ";
  }
  cout << endl;
}

int main(){
  vector<int> z;
  map<int,int> m;
  cin >> s;
  while(s!="."){
    int ans=0;
    int an=0;
    a.assign(52,m);
    c.assign(52,0);
    while(s!="."&&ans==0){
      b.assign(0,0);
      an++;
      int f=0;
      int e=0;
      t=0;
      for(int i=0;i<(int)s.size();i++){
        if(s[i]=='['){
          b.push_back(-64);
        }
        else if(s[i]==']'){
          b.push_back(-65);
          f=0;
        }
        else if(s[i]=='='){
          b.push_back(-66);
          e++;
        }
        else if('a'<=s[i]&&s[i]<='z'){
          b.push_back(-1-(s[i]-'a'));
        }
        else if('A'<=s[i]&&s[i]<='Z'){
          b.push_back(-1-(s[i]-'A'+26));
        }
        else if('0'<=s[i]&&s[i]<='9'){
          if(f==0){
            b.push_back(s[i]-'0');
            f++;
          }
          else{
            b[b.size()-1]*=10;
            b[b.size()-1]+=s[i]-'0';
          }
        }
      }
      if(e==0){
        int ra,rb;
        ra=-b[t]-1;
        t++;
        t++;
        rb=get_num();
        t++;
        if(rb>=0){
          //a[ra].assign(rb,-1);
          c[ra]=rb;
        }
        else{
          ans=an;
        }
      }
      else{
        int ra,rb,rc;
        ra=-b[t]-1;
        t++;
        t++;
        rb=get_num();
        t++;
        t++;
        rc=get_num();
        t++;
        if(rb<(int)c[ra]&&rc>=0){
          a[ra][rb]=rc+1;
        }
        else{
          ans=an;
        }
      }
      cin >> s;
    }
    cout << ans << endl;
    while(s!="."){
      cin >> s;
    }
    cin >> s;
  }
}

int get_num(){
  if(b[t]>=0){
    t++;
    return b[t-1];
  }
  else{
    int ra,rb;
    ra=-b[t]-1;
    t++;
    t++;
    rb=get_num();
    t++;
    if(rb<c[ra]){
      return a[ra][rb]-1;
    }
    else{
      return -1;
    }
  }
}

