### classes

class Group
  attr_accessor :id, :n, :reqs, :deps, :ms

  def initialize(_id)
    @id = _id; @n = 0; @reqs = []; @deps = []; @ms = []
  end
end

### main

loop do
  n = gets.to_i
  break if n == 0

  gids = {}
  gn = 0
  mlists = []

  n.times do
    gname, mlist = gets.strip.delete('.').split(':')
    gids[gname] = gn
    mlists << mlist
    gn += 1
  end
  #p gids
  #p mlists

  grps = (0...n).map{|id| Group.new(id)}
  mids = {}
  mn = 0

  for id in (0...n)
    grp = grps[id]

    for mem in mlists[id].split(',')
      gid = gids[mem]
      if gid.nil?
        if mids[mem].nil?
          mids[mem] = mn
          mn += 1
        end
        grp.ms << mids[mem]
      else
        grp.deps << gid
        grps[gid].reqs << id
      end
    end
  end
  #p grps

  q = grps.select{|g| g.deps.empty?}
  #p q

  while ! q.empty?
    grp = q.shift

    for req in grp.reqs
      greq = grps[req]
      greq.ms += grp.ms
      greq.ms.uniq!
      greq.n += 1

      if greq.n == greq.deps.length
        q << greq
      end
    end
  end
  #p grps

  puts grps.first.ms.length
end