#include<iostream>
#include<map>
#include<sstream>
#include<cstdlib>
using namespace std;

string Expr;
int P;
map<int,int>Value[128];
int Size[128];
bool Error;

void program();
void declaration();
void assignment();
int expression();
int number();
char array_name();

void program(){
  if(Expr.find("=",0)==string::npos){
    declaration();
  }else{
    assignment();
  }
  return;
}
void declaration(){
  char name=array_name();
  P++;
  int n=number();
  P++;
  Size[name]=n;
}
void assignment(){
  char name=array_name();
  P++;
  int ex1=expression();
  P++;
  P++;
  int ex2=expression();
  if(ex1>=Size[name]){
    Error=true;
  }
  Value[name][ex1]=ex2;
}
int expression(){
  int n;
  if(isdigit(Expr[P])){
    n=number();
  }else{
    char name=array_name();
    P++;
    n=expression();
    P++;
    if(n>=Size[name]){
      Error=true;
    }
    if(Value[name].find(n)==Value[name].end()){
      Error=true;
    }
    n=Value[name][n];
  }
  return n;
}
int number(){
  string n;
  while(isdigit(Expr[P]))n+=Expr[P++];
  return atoi(n.c_str());
}
char array_name(){
  return Expr[P++];
}

int main()
{
  while(cin>>Expr,Expr!="."){
    int ans=0;
    for(int i=0;i<128;i++){
      Value[i].clear();
      Size[i]=-1;
    }
    Error=false;
    do{
      if(Error)continue;
      P=0;
      program();
      ans++;
    }while(cin>>Expr,Expr!=".");
    if(Error){
      cout<<ans<<endl;
    }else{
      cout<<"0"<<endl;
    }
  }
}