program main
  implicit none
  integer :: K
  integer :: out
  integer :: l,m,n
  integer t1, t2, t_rate, t_max, diff
  read(*,*) K
  !call system_clock(t1)   ! 開始時を記録  
  out = 0
  do n = 1,k
     do m = 1, K
        do l = 1,K
           out = out + gcd(gcd(l,m),n)
        end do
     end do
  end do
  write(*,'(i0)') out
!!  call system_clock(t2, t_rate, t_max)
!!  if ( t2 < t1 ) then
!!    diff = (t_max - t1) + t2 + 1
!!  else
!!    diff = t2 - t1
!!  endif
!!  print "(A, F10.3)", "time it took was:", diff/dble(t_rate)    
  stop
contains
  function gcd(a,b) result(ret)
    integer :: a,b
    integer :: m,n
    integer :: k
    integer :: ret
    m = a
    n = b
    do
       k = mod(m, n)
       if (k == 0) then
          ret = n
          return 
       end if
       ! step 4
       m = n
       n = k
    end do
  end function gcd
end program main
