function! s:output()
  let this = {'lines': []}

  function! this.append(line) abort
    call add(self.lines, a:line)
  endfunction

  function! this.build() abort
    return join(self.lines, "\n")
  endfunction

  return this
endfunction

function! s:main(input) abort
  let o = s:output()

  let n = str2nr(a:input[0])
  let cells = {'1': 1}
  let q = [1]
  let r = n == 1 ? 1 : -1
  while len(q) > 0
    let c = remove(q, 0)
    let b = printf('%b', c)
    let move = len(b) - len(substitute(b, '1', '', 'g'))

    let left = c - move
    let right = c + move

    if left > 0 && !has_key(cells, left)
      let cells[left] = cells[c] + 1
      call add(q, left)
    endif

    if right < n && !has_key(cells, right)
      let cells[right] = cells[c] + 1
      call add(q, right)
    endif

    if right == n
      let r = cells[c] + 1
      break
    endif
  endwhile

  call o.append(r)
  return o.build()
endfunction

let s:input = getline(1, '$')
enew
put =s:main(s:input)
2,$print