a,b=0,1 gets.chop.chars{|c|x=c.ord-48 y=x/5;a,b=[a+y,b+2-y].min,[a+1+y,b+1-y].min y=x%5;a,b=[a+y,b+5-y].min,[a+1+y,b+4-y].min} p [a,b+1].min