#include <bits/stdc++.h>
using namespace std;
int main(){
  while (1){
    vector<string> S;
    while (1){
      string T;
      cin >> T;
      if (T == "."){
        break;
      }
      S.push_back(T);
    }
    int N = S.size();
    if (N == 0){
      break;
    }
    map<char, int> size;
    map<char, map<int, int>> array;
    int ans = 0;
    for (int i = 0; i < N; i++){
      int sz = S[i].size();
      int equal_pos = -1;
      for (int j = 0; j < sz; j++){
        if (S[i][j] == '='){
          equal_pos = j;
        }
      }
      if (equal_pos == -1){
        char array_name = S[i][0];
        int array_size = stoi(S[i].substr(2, S[i].size() - 3));
        size[array_name] = array_size;
      } else {
        stack<char> st1;
        int index1 = 0;
        for (int j = 0; j < equal_pos; j++){
          if (isalpha(S[i][j])){
            st1.push(S[i][j]);
          }
          if (isdigit(S[i][j])){
            index1 = index1 * 10 + S[i][j] - '0';
          }
        }
        stack<char> st2;
        int index2 = 0;
        for (int j = equal_pos; j < sz; j++){
          if (isalpha(S[i][j])){
            st2.push(S[i][j]);
          }
          if (isdigit(S[i][j])){
            index2 = index2 * 10 + S[i][j] - '0';
          }
        }
        while (st1.size() > 1){
          char array_name = st1.top();
          st1.pop();
          if (!array[array_name].count(index1)){
            ans = i + 1;
            break;
          } else {
            index1 = array[array_name][index1];
          }
        }
        while (!st2.empty()){
          char array_name = st2.top();
          st2.pop();
          if (!array[array_name].count(index2)){
            ans = i + 1;
            break;
          } else {
            index2 = array[array_name][index2];
          }
        }
        if (ans > 0){
          break;
        }
        char array_name = st1.top();
        if (index1 >= size[array_name]){
          ans = i + 1;
          break;
        } else {
          array[array_name][index1] = index2;
        }
      }
    }
    cout << ans << endl;
  }
}
