# frozen_string_literal: true # rubocop:todo Metrics/AbcSize # rubocop:todo Metrics/CyclomaticComplexity def solve # rubocop:todo Metrics/MethodLength return 1 if N == 1 positions = [0] squares = Array.new(N, nil) squares[0] = 0 (1..N).each do |step| positions = positions.map do |position| bit_count = BIT_COUNTS[position + 1] return step + 1 if (position + bit_count) == N - 1 [position - bit_count, position + bit_count].map do |i| if i >= 0 && i <= N - 1 && (squares[i].nil? || step < squares[i]) squares[i] = step i end end.compact end.flatten end -1 end # rubocop:enable Metrics/CyclomaticComplexity # rubocop:enable Metrics/AbcSize N = gets.to_i BIT_COUNTS = (0..N).map { _1.digits(2).sum } puts solve