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