### constant

MAX_N = 1024

### main

dists = []
st = 1
dists[st] = 0
q = [[st, [st]]]

while ! q.empty?
  u = q.shift
  ui, uds = u

  ud = dists[ui]
  nvd = ud + 1

  for d in uds
    vi = ui + d
    if vi <= MAX_N
      if dists[vi].nil?
        dists[vi] = nvd
        q << [vi, uds + [vi]]
      elsif dists[vi] == nvd
        q << [vi, uds + [vi]]
      end
    end

    vi = (ui - d).abs
    if vi >= 1
      if dists[vi].nil?
        dists[vi] = nvd
        q << [vi, uds + [vi]]
      elsif dists[vi] == nvd
        q << [vi, uds + [vi]]
      end
    end
  end
end
#p dists

loop do
  n = gets.to_i
  break if n == 0

  puts dists[n]
end