#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, kol;
  cin >> n;
  int ans = 0;
  map<string, int> st;
  st["POLYCARP"] = 1;
  for (int j = 1; j <= n; j++) {
    string s1, s2, s3;
    cin >> s1 >> s2 >> s3;
    for (int i = 0; i < s1.size(); i++)
      if (s1[i] >= 'a' && s1[i] <= 'z') s1[i] = s1[i] - 'a' + 'A';
    for (int i = 0; i < s3.size(); i++)
      if (s3[i] >= 'a' && s3[i] <= 'z') s3[i] = s3[i] - 'a' + 'A';
    st[s1] = max(st[s1], st[s3] + 1);
    ans = max(ans, st[s1]);
  }
  cout << ans;
  return 0;
}
