#include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(a)-1;i>=b;i--)
#define chmin(a,b) (a)=min((a),(b));
#define chmax(a,b) (a)=max((a),(b));
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define printV(v) cerr<<(#v)<<":";for(auto(x):(v)){cerr<<" "<<(x);}cerr<<endl;
#define printVS(vs) cerr<<(#vs)<<":"<<endl;for(auto(s):(vs)){cerr<<(s)<< endl;}
#define printVV(vv) cerr<<(#vv)<<":"<<endl;for(auto(v):(vv)){for(auto(x):(v)){cerr<<" "<<(x);}cerr<<endl;}
#define printP(p) cerr<<(#p)<<(p).first<<" "<<(p).second<<endl;
#define printVP(vp) cerr<<(#vp)<<":"<<endl;for(auto(p):(vp)){cerr<<(p).first<<" "<<(p).second<<endl;}

inline void output(){ cerr << endl; }
template<typename First, typename... Rest>
inline void output(const First& first, const Rest&... rest) {
    cerr << first << " "; output(rest...);
}

using ll = long long;
using Pii = pair<int, int>;
using TUPLE = tuple<int, int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
const int inf = 1ll << 60;
const int mod = 1e9 + 7;
using Graph = vector<vector<int>>;

class Array {
private:
    int len;
    map<int, int> mp;
public:
    Array() {}
    Array(int _len) {len = _len;}
    int access(int id) {
        if (id == -1 || id >= len) return -1;
        if (!mp.count(id)) return -1;
        return mp[id];
    }
    bool update(int id, int x) {
        if (id == -1 || id >= len || x == -1) return false;
        mp[id] = x;
        return true;
    }
};

using Arrays = map<string, Array>;

int p;

string array_name(const string& s) {
    string name = "";
    while (s[p] != '[') {
        name += s[p++];
    }
    p++;
    return name;
}

int number(const string& s){
    string str_num;
    while (isdigit(s[p])) {
        str_num += s[p++];
    }
    return stoi(str_num);
}

void declare(const string& s, Arrays& arrays) {
    p = 0;
    string name = array_name(s);
    int len = number(s);
    arrays[name] = Array(len);
}

int expr(const string& s, Arrays& arrays) {
    if (isdigit(s[p])) return number(s);
    string name = array_name(s);
    int id = expr(s, arrays);
    return arrays[name].access(id);
}

bool assign(const string& s, Arrays& arrays) {
    stringstream ss(s);
    string left, right;
    getline(ss, left, '=');
    getline(ss, right);

    p = 0;
    string name = array_name(left);
    int id = expr(left, arrays);

    p = 0;
    int x = expr(right, arrays);

    return arrays[name].update(id, x);
}

int solve(const vector<string>& vs) {
    Arrays arrays;
    rep(i, vs.size()) {
        if (vs[i].find('=') == string::npos) {
            declare(vs[i], arrays);
        }
        else {
            if (!assign(vs[i], arrays)) return i + 1;
        }
    }
    return 0;
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    while (true) {
        string s;
        vector<string> vs;
        while (cin >> s, s != ".") {
            vs.emplace_back(s);
        }
        if (vs.empty()) return 0;
        cout << solve(vs) << endl;
    }
}