#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
#define pb(a) push_back(a)
#define pr(a) cout<<(a)<<endl
#define PR(a,b) cout<<(a)<<" "<<(b)<<endl
#define R cin>>
#define F first
#define S second
#define ll long long
bool check(int n,int m,int x,int y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<60,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<char,int> P;
map<P,int> m;
map<char,int> mm;
int p,z;
string s;
string dfs() {
  if(p>=s.size() || s[p]==']') return "-2";
  if(isalpha(s[p])) {
    char c=s[p];
    p+=2;
    string d=dfs();
    stringstream ss,ss2;
    ss << d;
    int x;
    ss >> x;
    if(x==-1) return "-1";
    if(!mm.count(c)) return "-1";
    if(x>=mm[c]) return "-1";
    if(!m.count(P(c,x))) return "-1";
    ss2 << m[P(c,x)];
    ss2 >> d;
    return d;
  }
  string t="";
  t+=s[p];
  p++;
  string d=dfs();
  if(d=="-1") return "-1";
  if(d!="-2") t+=d;
  return t;
}

int solve() {
  p=2;
  string r=dfs();
  stringstream ss,ss2;
  ss << r;
  int d;
  ss >> d;
  if(d==-1) return -1;
  while(p<s.size() && s[p]==']') p++;
  if(p>=s.size()) {
    mm[s[0]]=d;
    return 1;
  }
  if(!mm.count(s[0])) return -1;
  if(d>=mm[s[0]]) return -1;
  if(s[p]=='=') p++;
  int c;
  if(isalpha(s[p])) {
    char ch=s[p];
    p+=2;
    r=dfs();
    ss2 << r;
    ss2 >> c;
    if(c==-1) return -1;
    if(!mm.count(ch)) return -1;
    if(c>=mm[ch]) return -1;
    if(!m.count(P(ch,c))) return -1;
    c=m[P(ch,c)];
  } else {
    r=dfs();
    ss2 << r;
    ss2 >> c;
  }
  m[P(s[0],d)]=c;
  return 1;
}

int main() {
  int ans=-1;
  while(1) {
    R s;
    if(s==".") {
      if(!z) break;
      if(ans==-1) pr(0);
      else pr(ans);
      ans=-1;
      z=0;
      m.clear();
      mm.clear();
      continue;
    }
    z++;
    if(solve()==-1) {
      if(ans==-1) ans=z;
    }
  }
  return 0;
}
