gets p (0...i=27e3).map{|n|*d=0 (1..30).map{d<<[d[~(n/900)]||i,d[~(n/30%30)]||i,d[~(n%30)]||i].min+1} eval$_.gsub /\d+/,'+d[\0]'}.min