#include<bits/stdc++.h>
using namespace std;
string s;
int len,pos,bug;
map<string,int> size;
map<string, map<int,int> > a;

int getNum(){
  int res=0;
  while('0'<=s[pos]&&s[pos]<='9')
    res=res*10+(s[pos++]-'0');
  return res;
}

string getName(){
  string res="";
  while(s[pos]!='[')res+=s[pos++];
  return res;
}

int solve1(){
  if('0'<=s[pos]&&s[pos]<='9')
    return getNum();
  string str=getName();
  pos++;
  int id=solve1();
  pos++;
  if(a[str].count(id)==0)bug=1;
  return a[str][id];
}

void solve0(){
  for(int i=0;i<(int)s.size();i++){
    if(s[i]=='['){
      pos=i+1;
      size[s.substr(0,i)]=getNum();
    }
  }
}

int solve(){
  int flg=-1;
  for(int i=0;i<(int)s.size();i++)
    if(s[i]=='=')flg=i;
  
  if(flg==-1){   solve0();  return false;  }
  
  string tmp=s;
  s=tmp.substr(flg+1);
  
  bug=pos=0;
  int res= solve1();
  if(bug)return true;
  
  bug=pos=0;
  s=tmp.substr(0,flg);
  string target=getName();
  pos++;
  int index=solve1();
  if(bug)return true;
  if(size[target]<=index||index<0)return true;
  a[ target ][ index ] = res;
  return false;
}

void init(){
  size.clear();
  a.clear();
}

int main(){
  while(1){
    init();
    int ans=0;
    for(int tc=1;;tc++){
      cin>>s;
      if(tc==1&&s==".")return 0;
      if(s==".")break;
      if(ans)continue;
      if(solve())ans=tc;
    }
    cout<<ans<<endl;
  }
  return 0;
}