#include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ll long long
#define lld long double
#define ALL(x) x.begin(), x.end()
#ifdef DEBUG
#define line() cerr << "[" << __LINE__ << "] ";
#define dump(i) cerr << #i ": " << i << " ";
#define dumpl(i) cerr << #i ": " << i << endl;
#else
#define line(i)
#define dump(i)
#define dumpl(i)
#endif
using namespace std;
map<char, int> arrsize;
map<char, map<int, bool>> isassigned;
map<char, map<int, int>> value;

bool bug = false;
typedef string::const_iterator State;
class ParseError
{
};

int number(State &begin)
{
    int ret = 0;

    while (isdigit(*begin))
    {
        ret *= 10;
        ret += *begin - '0';
        begin++;
    }
    return ret;
}

int term(State &begin)
{
    int ret = number(begin);

    for (;;)
    {
        if (*begin == '*')
        {
            begin++;
            ret *= number(begin);
        }
        else
        {
            break;
        }
    }
    return ret;
}

int index(State &begin)
{
    if (isdigit(*begin))
        return number(begin);
    char arrname = *begin;
    begin++;
    assert(*begin == '[');
    begin++;
    int ind = index(begin);
    assert(*begin == ']');
    begin++;
    if (isassigned[arrname][ind] == false || arrsize[arrname] <= ind)
    {
        bug = true;
        return -1;
    }
    return value[arrname][ind];
}

int assign(State &begin)
{
    char arrname = *begin;
    begin++;
    assert(*begin == '[');
    begin++;
    int ind = index(begin);
    if (bug == true)
    {
        return -1;
    }
    if (arrsize[arrname] <= ind)
    {
        bug = true;
        return -1;
    }
    begin++;
    assert(*begin == '=');
    begin++;
    int val = index(begin);
    if (bug == true)
    {
        return -1;
    }
    value[arrname][ind] = val;
    isassigned[arrname][ind] = true;
    return 0;
}

int decl(State &begin)
{
    char arrname = *begin;
    begin++;
    assert(*begin == '[');
    begin++;
    int sz = number(begin);
    assert(*begin == ']');
    begin++;
    arrsize[arrname] = sz;
    return 0;
}

bool isdecl(string s)
{
    rep(i, s.size())
    {
        if (s[i] == '=')
        {
            return false;
        }
    }
    return true;
}

int main(int argc, char const *argv[])
{
    bool ok = true;
    while (ok)
    {
        isassigned.clear();
        value.clear();
        arrsize.clear();
        int ans = 0;
        int now = 1;
        bug = false;
        ok = false;
        string s;
        while (cin >> s, s != ".")
        {
            ok = true;
            State begin = s.begin();
            if (isdecl(s))
            {
                decl(begin);
                if (bug == true && ans == 0)
                {
                    ans = now;
                }
            }
            else
            {
                assign(begin);
                if (bug == true && ans == 0)
                {
                    ans = now;
                }
            }
            now++;
        }
        if (ok == true)
            cout << ans << endl;
    }
    return 0;
}

