import deques, sequtils, strutils let n = parseInt readLine stdin let cnts = (0 .. n).mapIt toBin(it, 16).count '1' var dist = repeat(-1, succ n) dist[1] = 1 var q = toDeque [1] while q.len > 0: var i = popFirst q let i2 = [i.pred cnts[i], i.succ cnts[i]] for next in i2: if next > 1 and next < n.succ: if dist[next] == -1: dist[next] = dist[i] + 1 q.addLast next echo dist[n]