#include <bits/stdc++.h>
using namespace std;
int n, ans;
char s1[30], s2[30];
map<string, vector<string> > mp;
map<string, int> dis;
map<string, bool> vis;
void solve() {
  queue<string> q;
  q.push("POLYCARP");
  dis["POLYCARP"] = 1;
  while (!q.empty()) {
    string p = q.front();
    q.pop();
    for (string ch : mp[p]) {
      if (!vis[ch]) {
        q.push(ch);
        ans = max(dis[ch] = dis[p] + 1, ans);
        vis[ch] = 1;
      }
    }
  }
}
void up(char x[]) {
  for (int i = 0; i < strlen(x); i++) {
    if (x[i] > 'Z') x[i] += (-'a' + 'A');
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%s reposted %s", &s1, &s2);
    up(s1);
    up(s2);
    mp[s2].push_back(s1);
  }
  solve();
  cout << ans << "\n";
  return 0;
}
