program main
  implicit none
  integer :: N
  integer,allocatable :: A(:)
  integer,allocatable :: A2(:)
  integer :: Amin, i_loop,Amin_bf
  integer :: i,j,k
  read(*,*) N
  allocate(A(N),A2(N))
  read(*,*) A
  A2 = A
  i_loop = minval(A)
  ! calc mod
  do ! i = 1,i_loop
     Amin_bf = minval(A,A >0 )
     A = mod(A2,minval(A,A >0 ))
     if ( all(A == 0) ) exit
     Amin = minval(A,A>0)
     A2 = A + Amin_bf - Amin
  end do
  write(*,'(i0)') minval(A2)
  deallocate(A)
  deallocate(A2)
  stop
contains
  recursive function iqsort(ix) result(ires)
        integer, allocatable :: ires(:)
        integer, intent(in)  :: ix(:)
        integer :: k
        if (size(ix) <= 1) then 
          ires = ix
        else  
          k = ix(size(ix) / 2)
          ires = [iqsort(pack(ix, ix < k)), pack(ix, ix == k), iqsort(pack(ix, ix > k))] 
        end if
      end function iqsort  
end program main
