#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
void solve() {
  long long n, i, j, x, y, maxm = -1;
  map<string, long long> make_pair;
  make_pair["POLYCARP"] = 1;
  cin >> n;
  string s1, s2, s3;
  for (i = 0; i < n; i++) {
    cin >> s1 >> s2 >> s3;
    transform(s1.begin(), s1.end(), s1.begin(), ::toupper);
    transform(s3.begin(), s3.end(), s3.begin(), ::toupper);
    make_pair[s1] = make_pair[s3] + 1;
    if (make_pair[s1] > maxm) maxm = make_pair[s1];
  }
  cout << maxm << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}
