n=int(input())
name=[]
tmp=[]
def run(x,y,z):
for i in range(len(tree)):
if tree[x][i]==1 and i!=y:
run(i,x,z+1)
zzz.append(z)
ok.append(name[x])
for i in range(n):
m=list(map(str,input().split()))
tmp.append(m)
name.append(m[0])
for j in range(1,len(m)):
name.append(m[j])
name=list(set(name))
tree=[[0 for i in range(len(name))]for j in range(len(name))]
for i in range(len(tmp)):
for j in range(1,len(tmp[i])):
tree[name.index(tmp[i][0])][name.index(tmp[i][j])]=1
tree[name.index(tmp[i][j])][name.index(tmp[i][0])]=1
x,y=map(str,input().split())
zzz=[]
ok=[]
run(name.index(x),name.index(x),0)
print(zzz[ok.index(y)])