#include <cstdio>
#include <string>
#include <iostream>
#include <map>
using namespace std;
using ll = long long int;

const int INF = 1 << 28;
struct Array {
    ll lim;
    map<ll, ll> rec;
};

ll str_eval(string s, map<char, Array>& mp, int l, int r) {
    if(isalpha(s[l])) {
        // assignment
        char array_name = s[l];

        int i = l, sum = 0;
        while(!(sum == 1 && s[i] == ']')) {
            if(s[i] == '[') sum++;
            if(s[i] == ']') sum--;
            i++;
        }

        ll idx = str_eval(s, mp, l+2, i);

        if(i+1 == r || s[i+1] != '=') {
            if(!mp[array_name].rec.count(idx)) return -1;
            return mp[array_name].rec[idx];
        }
        else {
            ll value = str_eval(s, mp, i+2, r);

            if(idx < 0 || value < 0) return -1;
            if(mp[array_name].lim <= idx) return -1;
            return mp[array_name].rec[idx] = value;
        }
    }
    else {
        // number
        ll num = 0;
        for(int i=l; i<r; i++) num = num * 10 + (s[i] - '0');
        return num;
    }
}

int main() {
    while(1) {
        int z = 0, ans = INF;
        map<char, Array> mp;

        while(1) {
            string s; cin >> s;
            if(z == 0 && s == ".") goto program_end;
            if(s == ".") break;

            z++;
            char array_name = s[0];
            if(!mp.count(array_name)) {
                // declaration
                ll range = 0;
                for(size_t i=1; i<s.size(); i++) {
                    if(!isdigit(s[i])) continue;
                    range = range * 10 + (s[i] - '0');
                }
                mp[array_name] = Array{range, map<ll, ll>()};
            }
            else {
                // assignment
                ll res = str_eval(s, mp, 0, s.size());
                if(res < 0) {
                    ans = min(ans, z);
                }
            }
        }

        cout << (ans == INF ? 0 : ans) << endl;
    }

    program_end:;
    return 0;
}
