#include <bits/stdc++.h>
using namespace std;
unordered_map<string, vector<string>> adj;
unordered_map<string, bool> vis;
int height = 0;
int ans = -1;
void dfs(string root) {
  height++;
  if (adj[root].size() == 0) {
    ans = max(ans, height);
  }
  for (auto x : adj[root]) {
    if (!vis[x]) {
      vis[x] = true;
      dfs(x);
      height--;
    }
  }
}
int main() {
  long long int n;
  cin >> n;
  while (n--) {
    string s, k, l;
    cin >> s >> k >> l;
    transform(s.begin(), s.end(), s.begin(), ::toupper);
    transform(l.begin(), l.end(), l.begin(), ::toupper);
    adj[l].push_back(s);
    vis[s] = false;
    vis[l] = false;
  }
  vis["POLYCARP"] = true;
  dfs("POLYCARP");
  cout << ans;
  return 0;
}
