def g;gets.split.map(&:to_i)end n,m=g l=Hash.new{|h,k|h[k]=Hash.new{[]}} m.times{ a,b,c=g l[c][a]+=[b] l[c][b]+=[a] } now=*1..n g.each{|c|p now=l[c].values_at(*now).flatten.uniq} puts now.size,now.sort*" "