gets p (0..3e4).map{|n|*d=0 30.times{d<<(0..2).map{|x|d[~(n/30**x%30)]||$$}.min+1} eval$_.gsub /\d+/,'+d[\0]'}.min