#include<cstdio>
#include<map>
#include<string>
#include<iostream>
#include<queue>
#include<cstring>
const int MAXN=50000*3+5;
const int MAXM=150000000+5;
struct Edge{int u,v,next;};
int head[MAXN],cnt,nodecnt,dis[MAXN];bool vis[MAXN];Edge edge[MAXM];std::string inp;
std::map<std::string,int>map;
std::queue<int>que;
std::map<std::string,int>::iterator it;
void AddEdge(int u,int v)
{
int &i=++cnt;
edge[i].u=u;edge[i].v=v;edge[i].next=head[u];head[u]=i;
}
void SPFA(int start)
{
memset(dis,0x3f3f3f,sizeof(dis));
que.push(start);dis[start]=0;
while(!que.empty())
{
int u=que.front();que.pop();
vis[u]=0;
for(int i=head[u];i;i=edge[i].next)
{
int v=edge[i].v;
if(dis[v]>dis[u]+1)
{
dis[v]=dis[u]+1;
if(!vis[v])
{
vis[v]=1;
que.push(v);
}
}
}
}
}
int main()
{
int n,name[3],x,y;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
for(int j=0;j<3;j++)
{
std::cin>>inp;
x=map[inp];
if(x==0) x=map[inp]=++nodecnt;
name[j]=x;
}
for(int j=0;j<3;j++)
for(int k=j+1;k<3;k++)
{
x=name[j];y=name[k];
AddEdge(x,y);AddEdge(y,x);
}
}
SPFA(map["Xiaodao"]);
for(it=map.begin();it!=map.end();it++)
{
std::cout<<it->first<<" ";
if(dis[it->second]==dis[0]) printf("undefined\n"); else printf("%d\n",dis[it->second]);
}
return 0;
}