#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using vi = vector<ll>;
using vvi = vector<vector<ll>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-10;
const ll MOD = 1000000007;
const ll INF = 1000000000;
const ll LINF = 1ll<<50;
template<typename T>
void printv(const vector<T>& s) {
  for(ll i=0;i<(ll)(s.size());++i) {
    cout << s[i];
    if(i == (ll)(s.size())-1) cout << endl;
    else cout << " ";
  }
}
template<typename T1, typename T2>
ostream& operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}
void program(const string &s);
void declaration(const string &s);
void assignment(const string &s);
ll expression(const string &s);
map<char, map<ll, ll>> mp;
map<char, ll> sz;
bool bug = false;
ll mi = INF;
void program(const string &s) {
  bool decl = true;
  for(ll i=0;i<(ll)(s.size());++i) {
    if(s[i] == '=') {
      decl = false;
      break;
    }
  }
  if(decl) declaration(s);
  else assignment(s);
}
void declaration(const string &s) {
  char name = s[0];
  ll num = stoi(s.substr(2, (ll)(s.size())-3));
  sz[name] = num;
}
void assignment(const string &s) {
  ll eqpos = -1;
  for(ll i=0;i<(ll)(s.size());++i) {
    if(s[i] == '=') {
      eqpos = i;
      break;
    }
  }
  char name_left = s[0];
  ll num_left = expression(s.substr(2, eqpos-3));
  ll num_right = expression(s.substr(eqpos+1, (ll)(s.size())-eqpos-1));
  if(num_left == -1 || sz[name_left] <= num_left) {
    bug = true;
  } else {
    mp[name_left][num_left] = num_right + 1;
  }
}
ll expression(const string &s) {
  bool arr = false;
  for(ll i=0;i<(ll)(s.size());++i) {
    if(s[i] == '[') {
      arr = true;
      break;
    }
  }
  if(!arr) {
    return stoll(s);
  } else {
    char name = s[0];
    ll num = expression(s.substr(2, (ll)(s.size())-3));
    if(num == -1 || sz[name] <= num || mp[name][num] == 0) {
      bug = true;
      return -1;
    }
    return mp[name][num]-1;
  }
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  bool init = true;
  ll cnt = 0;
  while(1) {
    string s; cin >> s;
    if(s == "." && init) break;
    cnt++;
    init = false;
    if(s == ".") {
      cout << (mi == INF ? 0 : mi) << endl;
      mp.clear();
      bug = false;
      cnt = 0;
      mi = INF;
      init = true;
      continue;
    }
    if(!bug) program(s);
    if(bug) {
      mi = min(mi, cnt);
    }
  }
}

