#define  _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#pragma comment (linker, "/STACK:526000000")

#include "bits/stdc++.h"

using namespace std;
typedef string::const_iterator State;
#define eps 1e-11L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL

#define MOD 998244353LL
#define seg_size 262144
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

void init() {
    iostream::sync_with_stdio(false);
    cout << fixed << setprecision(20);
}


unsigned long xor128() {
    static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123;
    unsigned long t = (x ^ (x << 11));
    x = y; y = z; z = w;
    return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}


#define int long long

map<string, int> zip;
map<int, int> def[10000];
int cnt[300000];
int num(State& now) {
    int ans = 0;
    while (*now >= '0' && *now <= '9') {
        ans *= 10;
        ans += *now - '0';
        now++;
    }
    return ans;
}

int eval_right(State& now) {
    if (*now >= '0' && *now <= '9') {
        return num(now);
    }
    string b;
    while (*now != '[') {
        b.push_back(*now);
        now++;
    }
    now++;
    int hoge = eval_right(now);
    now++;
    if (hoge == -1) return -1;
    if (zip.count(b) == 0) return -1;
    if (def[zip[b]].count(hoge) == 0) return -1;
    return def[zip[b]][hoge];
}

void solve() {
    while (true) {
        string s;
        cin >> s;
        if (s == ".") return;
        vector<string> inputs;
        while (true) {
            inputs.push_back(s);
            cin >> s;
            if (s == ".") break;
        }
        REP(i, 10000) {
            cnt[i] = 0;
            def[i].clear();
        }
        zip.clear();
        int ans = 0;
        for (int i = 0; i < inputs.size(); ++i) {
            vector<string> syori;
            string tmp;
            REP(q, inputs[i].size()) {
                if (inputs[i][q] == '=') {
                    tmp.push_back(']');
                    syori.push_back(tmp);
                    tmp.clear();
                }
                else {
                    tmp.push_back(inputs[i][q]);
                }
            }
            tmp.push_back(']');
            syori.push_back(tmp);
            
            if (syori.size() == 1) {
                //definition
                string b;
                int cnter = 0;
                for (int q = 0; q < syori[0].size(); ++q) {
                    if (syori[0][q] == '[') {
                        State hoge = syori[0].begin() + q + 1;
                        cnter = num(hoge);
                        break;
                    }
                    b.push_back(syori[0][q]);
                }
                zip[b] = zip.size();
                cnt[zip[b]] = cnter;
            }
            else {
                State a = syori[1].begin();
                int val = eval_right(a);
                if (val == -1) {
                    ans = i + 1;
                    break;
                }
                string b;
                int cnter = 0;
                for (int q = 0; q < syori[0].size(); ++q) {
                    if (syori[0][q] == '[') {
                        State hoge = syori[0].begin() + q + 1;
                        cnter = eval_right(hoge);
                        break;
                    }
                    b.push_back(syori[0][q]);
                }
                if (cnter == -1) {
                    ans = i + 1;
                    break;
                }
                if (cnter >= cnt[zip[b]]) {
                    ans = i + 1;
                    break;
                }
                def[zip[b]][cnter] = val;
            }
        }
        cout << ans << endl;
    }
}

#undef int
int main() {
    init();
    solve();
}


Measured memory_bytes: 3832