← Home
```go
package main

import (
	"fmt"
)

func main() {
	var h, n int64
	fmt.Scan(&h, &n)
	fmt.Println(solve(h, n, true))
}

func solve(h, n int64, isLeft bool) int64 {
	if h == 0 {
		return 0
	}

	// Number of leaves in the left subtree of the current node
	mid := int64(1) << (h - 1)
	// Number of nodes in a perfect binary tree of height h-1
	subtreeSize := (int64(1) << h) - 1

	if isLeft {
		if n <= mid {
			// Expected Left, Target is Left. Correct path.
			// Next command will be Right (!isLeft).
			return 1 + solve(h-1, n, !isLeft)
		} else {
			// Expected Left, Target is Right. Wrong path.
			// Visit entire Left subtree, then go Right.
			// Command parity is preserved for the correct child (isLeft).
			return subtreeSize + 1 + solve(h-1, n-mid, isLeft)
		}
	} else {
		if n > mid {
			// Expected Right, Target is Right. Correct path.
			// Next command will be Left (!isLeft).
			return 1 + solve(h-1, n-mid, !isLeft)
		} else {
			// Expected Right, Target is Left. Wrong path.
			// Visit entire Right subtree, then go Left.
			// Command parity is preserved for the correct child (isLeft).
			return subtreeSize + 1 + solve(h-1, n, isLeft)
		}
	}
}
```