package main import ( "fmt" "math/bits" ) func main() { var n int fmt.Scan(&n) if n == 1 { fmt.Println(1) return } bfs := make([]int, 0) bfs = append(bfs, 1) distance, find := 1, false mp := make(map[int]int) mp[1] = 1 for len(bfs) > 0 { if find { break } length := len(bfs) for i := 0; i < length; i++ { if find { break } cur := bfs[0] bfs = bfs[1:] count := bits.OnesCount(uint(cur)) nexts := []int{cur + count, cur - count} for _, next := range nexts { if next == n { find = true break } else if next >= 1 && next < n && mp[next] == 0 { bfs = append(bfs, next) mp[next] = 1 } } } distance++ } if find { fmt.Println(distance) } else { fmt.Println(-1) } }