n = gets.chomp tot = 2 tot2 = 1 qq = [3,1,1,1,3,1,3,1,4,3] qq2 = [3,2,2,2,3,2,3,2,4,3] all1 = true for c in n.split('') tot += qq[c.ord - 48] all1 &= qq[c.ord - 48] == 1 tot2 += qq2[c.ord - 48] end if all1 tot2 = [tot2, n.size + 2].min end puts tot2