#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define int long long
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define all(x) x.begin(), x.end()
using pii = pair<int, int>;
constexpr int MOD = 1e9 + 7;
typedef string::const_iterator State;

map<int, int> mp[60];

bool exist[60];
bool wrong;
int number(State &begin);
int factor(State &begin) {
    if (islower(*begin)) {
        int num = *begin - 'a';
        if (!exist[num]) {
            wrong = true;
            return 0;
        }
        begin++;
        begin++;
        int ret = factor(begin);
        begin++;
        if (!mp[num].count(ret)) {
            wrong = true;
            return 0;
        }
        return mp[num][ret];
    } else if (isupper(*begin)) {
        int num = *begin - 'A' + 26;
        if (!exist[num]) {
            wrong = true;
            return 0;
        }
        begin++;
        begin++;
        int ret = factor(begin);
        begin++;
        if (!mp[num].count(ret)) {
            wrong = true;
            return 0;
        }
        return mp[num][ret];
    } else {
        return number(begin);
    }
}
int number(State &begin) {
    int ret = 0;
    while (isdigit(*begin)) {
        ret *= 10;
        ret += *begin - '0';
        begin++;
    }
    return ret;
}
bool no(int t) {
    cout << t << endl;
    return true;
}
int siz[60];
bool solve() {
    REP(i, 60) exist[i] = false;
    REP(i, 60) {
        siz[i] = 0;
        mp[i].clear();
    }

    string S;
    cin >> S;

    if (S == ".") return false;
    int t = 0;
    int ans = 0;
    while (true) {
        t++;
        wrong = false;
        //宣言
        if (!count(all(S), '=')) {
            if (islower(S[0])) {
                int num = S[0] - 'a';
                State begin = S.begin();
                begin++;
                begin++;
                int sz = factor(begin);
                if (wrong) {
                    if (!ans) ans = t;
                }
                siz[num] = sz;
                exist[num] = true;
            } else if (isupper(S[0])) {
                int num = S[0] - 'A' + 26;
                State begin = S.begin();
                begin++;
                begin++;
                int sz = factor(begin);
                if (wrong) {
                    if (!ans) ans = t;
                }
                siz[num] = sz;
                exist[num] = true;
            }
        } else {
            string l, r;
            int pos = find(all(S), '=') - S.begin();
            int n = S.size();
            REP(i, pos) l += S[i];
            FOR(i, pos + 1, n) r += S[i];
            // cerr << l << " " << r << endl;
            if (islower(S[0])) {
                int num = S[0] - 'a';
                if (!exist[num]) wrong = true;
                State begin = l.begin();
                begin++;
                begin++;
                int pos = factor(begin);
                if (pos >= siz[num]) wrong = true;

                State beginR = r.begin();
                int val = factor(beginR);
                mp[num][pos] = val;
            } else if (isupper(S[0])) {
                int num = S[0] - 'A' + 26;
                if (!exist[num]) wrong = true;
                State begin = l.begin();
                begin++;
                begin++;
                int pos = factor(begin);
                if (pos >= siz[num]) wrong = true;

                State beginR = r.begin();
                int val = factor(beginR);
                mp[num][pos] = val;
            }
        }
        if (wrong && !ans) ans = t;
        cin >> S;
        if (S == ".") {
            cout << ans << endl;
            return true;
            break;
        }
    }
    return true;
}

signed main() {
    while (solve())
        ;
}


Measured memory_bytes: 3212