def ascan; gets.split.map(&:to_i); end N, M = ascan params = nil degree = [0]*N degree[0] = M count = 1 N.times do |i| player = ascan if !params params = player.map{|e| [e,[i]]} else M.times do |j| if params[j][0] < player[j] params[j][1].each do |id| count -= 1 if (degree[id] -= 1) == 0 end params[j] = [player[j], []] end if params[j][0] <= player[j] params[j][1] << i count += 1 if (degree[i] += 1) == 1 end end end p count end