#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const long double EPS = 1e-10;
const long long INF = 1e18;
const long double PI = acos(-1.0L);
//const ll mod = 1000000007;
vector<string> S;
map<string, map<int, int>> mp;
map<string, int> sz;
typedef pair<string, int> si;

int EqualIdx(string S) {
    for(int i = 0; i < S.size(); i++) {
        if(S[i] == '=') return i;
    }
    return -1;
}

typedef string::iterator State;

bool IsDigit(State &itr) {
    return '0' <= *itr and *itr <= '9';
}

int Number(State &itr) {
    int ret = 0;
    while(true) {
        if(!IsDigit(itr)) {
            //cerr << "Number: " << ret << endl;
            return ret;
        }
        ret = ret * 10 + (int)(*itr - '0');
        itr++;
    }
}

int val(si x) {
    //cerr << "val: " << x.first << " " << x.second << endl;
    if(x.second < 0) return x.second;
    if(x.first == ".") return x.second;
    if(sz.count(x.first) == 0) return -1;
    //cerr << "sz: " << sz[x.first] << endl;
    if(sz[x.first] <= x.second) return -1;
    if(mp[x.first].count(x.second) == 0) return -1;
    //cerr << "ok: " << endl;
    return mp[x.first][x.second];
}

si f(State &itr) {
    //cerr << "f: " << *itr << endl;
    if(IsDigit(itr)) {
        auto ret = Number(itr);
        itr++;
        return {".", ret};
    }
    string fi;
    while(*itr != '[') {
        fi.push_back(*itr);
        itr++;
    }
    itr++;
    auto Inside = f(itr);
    itr++;
    //if(Inside.first == ".") return {fi, Inside.second};
    //cerr << "Inside: " << Inside.first << " " << Inside.second << endl;
    return {fi, val(Inside)};
    //return {fi, num};
}

int main() {
    while(true) {
        S.clear();
        mp.clear();
        sz.clear();
        while(true) {
            string tmp;
            cin >> tmp;
            if(tmp == ".") break;
            S.push_back(tmp);
        }
        if(S.empty()) break;
        int ans = 0;
        for(int i = 0; i < S.size(); i++) {
            //cerr << "----" << i << "----" << endl;
            int idx = EqualIdx(S[i]);
            if(idx >= 0) {
                auto itr = S[i].begin();
                auto Left = f(itr);
                itr = S[i].begin();
                itr += idx + 1;
                auto Right = f(itr);
                if(Right.second < 0) {
                    ans = i + 1;
                    break;
                }
                //cerr << "A" << endl;
                //cerr << Right.first << " " << Right.second << endl;
                int Value = val(Right);
                //cerr << Right.first << " " << Right.second << " " << Value << endl;
                if(Value < 0) {
                    ans = i + 1;
                    break;
                }
                //cerr << "In: " << Left.first << " " << Left.second << " " << Value << endl;
                if(sz[Left.first] <= Left.second) {
                    ans = i + 1;
                    break;
                }
                mp[Left.first][Left.second] = Value;
                //cerr << Left.first << " " << Left.second << " " << Right.first << " " << Right.second << endl;
                //cerr << Left.first << " " << Left.second << " ---val--- " << Value << endl;
            } else {
                auto itr = S[i].begin();
                auto Main = f(itr);
                sz[Main.first] = Main.second;
                mp[Main.first][-2] = -1;
                //cerr << Main.first << " " << Main.second << endl;
            }
        }
        cout << ans << endl;
    }
    return 0;
}
