#include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

class C{
  public:
    int len;
    map<int, int> v;
    C(){}
    C(int _len){ len = _len; }
};

string str;
map<char, C> m;

void def(int l, int r){
  char c = str[l];
  int n = atoi(str.substr(l + 2, r - 2).c_str());
  m[c] = C(n);
}

int getter(int l, int r, char c, int d){
  if(str[r - 1] != ']'){
    int n = atoi(str.substr(l, r - l).c_str());
    if(c == '!') return n;
    if(m.find(c) == m.end() || n >= m[c].len || m[c].v.find(n) == m[c].v.end()) return -1;
    return m[c].v[n];
  }
  int res = getter(l + 2, r - 1, str[l], d + 1);
  if(d == 0) return res;
  if(res == -1 || m.find(c) == m.end() || res >= m[c].len) return -1;
  return m[c].v[res];
}

int setter(int l, int r, int num, int d){
  if(str[r - 1] != ']') return atoi(str.substr(l, r - l).c_str());
  int res = setter(l + 2, r - 1, num, d + 1);
  char c = str[l];
  if(res == -1 || m.find(c) == m.end() || res >= m[c].len) return -1;
  if(d == 0) m[c].v[res] = num;
  return m[c].v[res];
}

bool solve(){
  int res = 1;
  bool f = false;
  REP(i, str.length()){
    if(str[i] == '='){
      int g = getter(i + 1, (int)str.length(), (isdigit(str[i + 1]) ? '!' : str[i + 1]), 0);
      if(g == -1) return false;
      int s = setter(0, i, g, 0);
      if(s == -1) return false;
      return true;
    }
  }
  def(0, (int)str.length());
  return true;
}

int main() {
  int ans = 0, f = 0;
  while(cin >> str){
    if(ans == 0 && str == ".") break;
    ++ans;
    if(str == ".") { cout <<f <<endl; ans = 0; f = 0; m.clear(); continue; }
    if(f == 0 && !solve()) f = ans;
  }
  return 0;
}