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|now=l[c].values_at(*now).flatten} puts now.size,now*" "