open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

open InputUtility

module AGC015 =
    let private ``A or...or B Problem`` () =
        let ub = 60
        let left x b = b - x + 1L
        let rec cal x y i =
            if i < 0 then 1L
            else
                let b = 1L <<< i
                let bx, by = x &&& b, y &&& b
                if bx = by then cal x y (i - 1)
                elif i = 0 then 2L
                else
                    let nb = 1L <<< (i - 1)
                    let c = b - 1L
                    let f z isLeft =
                        let e = z &&& c
                        let rec inner x =
                            let s = x &&& -x
                            if x > s then inner (x - s)
                            elif x = 0L then 1L
                            elif isLeft then c - e + 1L
                            else 2L * x
                        inner (e ^^^ c)
                    let fy () = f (~~~y) false
                    let nx, ny = x &&& nb, y &&& nb
                    if ny = nb then 2L * b - (x &&& c)
                    elif nx = 0L then
                        let rx = nb - (x &&& c)
                        let ry = fy () + rx |> min <| nb
                        rx + b + ry
                    else 2L * f x true + fy ()
        let g = ReadNext >> int64
        let a, b = g (), g ()
        cal a b ub |> printfn "%d"

    let Entry = ``A or...or B Problem``

AGC015.Entry ()