#include <bits/stdc++.h>

using namespace std;

namespace {

    typedef long long ll;

    template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {
        if (vs.empty()) return os << "[]";
        os << "[" << vs[0];
        for (int i = 1; i < vs.size(); i++) os << " " << vs[i];
        return os << "]";
    }

    ll to_i(const string& s) {
        istringstream is(s);
        ll n; is >> n;
        return n;
    }

    string s;
    bool valid;

    map< char, map<ll, ll> > M;
    map< char, ll > L;
    
    const string E = "ERROR";

    ll num(int& i) {
        assert(isdigit(s[i]));
        string n;
        while (i < s.size() && isdigit(s[i])) {
            n.push_back(s[i]);
            i++;
        }
        return to_i(n);
    }
    ll expr(int& i) {
        if (isdigit(s[i])) {
            return num(i);
        } else {
            char name = s[i++];
            assert(s[i] == '[');
            i++;
            ll v = expr(i);
            assert(s[i] == ']');
            i++;
            if (not M.count(name)) throw E;
            if (not M[name].count(v)) throw E;
            return M[name][v];
        }
    }
    void decl(int& i) {
        char name = s[i++];
        assert(s[i] == '[');
        i++;
        ll len = expr(i);
        assert(s[i] == ']');
        i++;
        L[name] = len;
    }
    void assign(int& i) {
        char name = s[i++];
        if (not L.count(name)) throw E;
        assert(s[i] == '[');
        i++;
        ll index = expr(i);
        assert(s[i] == ']');
        i++;
        assert(s[i] == '=');
        i++;
        ll v = expr(i);
        assert(i == s.size());
        i++;
        if (L[name] <= index) throw E;
        M[name][index] = v;
    }

    void parse() {
        bool d = true;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == '=') {
                d = false;
                break;
            }
        }
        int i = 0;
        if (d) decl(i);
        else assign(i);
    }

    bool solve() {
        M.clear();
        L.clear();
        s = "";
        valid = false;
        getline(cin, s);
        if (s[0] == '.') return false;
        int t = 0;
        bool has_error = false;
        do {
            if (s[0] == '.') break;
            try {
                parse();
            } catch(string e) {
                cout << t + 1 << endl;
                has_error = true;
                while (true) {
                    getline(cin, s);
                    if (s[0] == '.') break;
                }
                break;
            }
            t++;
            getline(cin, s);
        } while (true);
        if (!has_error) {
            cout << 0 << endl;
        }
        return true;
    }
}

int main() {
    while (solve()) ;
    return 0;
}