program main implicit none integer::N,D,i,curr,next integer,allocatable::ashiba(:) read *,N,D allocate(ashiba(0:N-1)) ashiba = 1 curr = 0 do i=1,N ashiba(curr) = 0 next = MOD(curr+D,N) if(ashiba(next).eq.0) exit curr = next end do i = MIN(N,i) print '(i0)',i end program main