// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):x)
#define uint unsigned int
#define dbl long double
using namespace std;
// mylittledoge
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
string s;
map<string,int> M;
vector< vector<int> > dep;
vector<int> name;
vector<int> akt;
while(cin >> s) {
if(s == "0") {
dep.push_back(akt);
akt.clear();}
else {
int x =(M.find(s) == M.end())?M.size():M[s];
M[s] =x;
if(name.size() == dep.size()) name.push_back(x);
else akt.push_back(x);}
}
int N =M.size();
vector< vector<int> > G(N);
vector<int> D(N,-OVER9000);
for(int i =0; i < name.size(); i++) D[name[i]] =0;
for(int i =0; i < dep.size(); i++) ALL_THE(dep[i],it) {
G[*it].push_back(name[i]);
D[name[i]]++;}
queue<int> q;
for(int i =0; i < N; i++) if(D[i] == 0) q.push(i);
int ans =0;
while(!q.empty()) {
ans++;
int a =q.front();
ALL_THE(G[a],it) {
D[*it]--;
if(D[*it] == 0) q.push(*it);}
q.pop();}
cout << ans << "\n";
return 0;}
// look at my code
// my code is amazing
Ly8gaW9zdHJlYW0gaXMgdG9vIG1haW5zdHJlYW0KI2luY2x1ZGUgPGNzdGRpbz4KLy8gYml0Y2ggcGxlYXNlCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDx2ZWN0b3I+CiNpbmNsdWRlIDxzZXQ+CiNpbmNsdWRlIDxtYXA+CiNpbmNsdWRlIDxxdWV1ZT4KI2luY2x1ZGUgPHN0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