program main
    implicit none
    integer::N,X,v(2),point(2),count=0
    logical,allocatable::map(:,:)
    read*,N,X
    allocate(map(0:N,0:N))

    call set(N,map)
    point=(/0,X/)
    v=(/1,0/)

    do
        call move(N,map,point,v,count)
        if(point(1)==0.and.point(2)==X)then
            exit
        end if
    end do
    print*,count
end program main

subroutine set(N,map)
    implicit none
    integer,intent(in)::N
    logical,intent(out)::map(0:N,0:N)
    integer::i,j
    do i=0,N
        do j=0,N-i
            if(i==0.or.j==0)then
                map(i,j)=.false.
            else if(i+j==N)then
                map(i,j)=.false.
            else
                map(i,j)=.true.
            end if
        end do
    end do
end subroutine set
subroutine move(N,map,point,v,count)
    implicit none
    integer,intent(in)::N
    integer,intent(inout)::point(2),v(2),count
    logical,intent(inout)::map(0:N,0:N)
    integer::i
    do i=1,2
        point(i)=point(i)+v(i)
    end do
    !直進
    if(map(point(1),point(2)))then
        map(point(1),point(2))=.false.
    !反射
    else
        !右に向かっていた場合（右下はない）
        if(v(1)==1)then
            if(point(1)+point(2)==N)then
                v(1) = 0
                v(2) =-1
            else
                v(1) =-1
                v(2) = 1
            end if
        !左に向かっていた場合＝左上方向のみ、天井に当たるのと左の壁に当たるのと
        else if(v(1)==-1)then
            if(point(1)==0)then
                v(1)=1
                v(2)=0
            else
                v(1)=0
                v(2)=-1
            end if
        !下方向
        else
            v(1)=-1
            v(2)=1
        end if
    end if
    count=count+1
end subroutine move