      module gcdMod
      implicit none
      
      !reference!
!https://blog.pixela.co.jp/2017/11/16/ユークリッドの互除法/
      
      contains
      recursive function gcd( a,b ) result( c )
        implicit none
        integer(16),intent(in) :: a,b
        integer(16) :: c,bigger,smaller
        integer(16) :: r
        
        bigger = max( a,b )
        smaller= min( a,b )
        
        r = mod(bigger,smaller)
        if( r==0 )then
          c = smaller
        else
          c = gcd( r,smaller )
        end if
      end function
      
      subroutine sort( array )
        implicit none
        integer(16),intent(inout) :: array(3)
        integer(16) :: i,j,buffer
        
        do i = 1,2
          do j = 2,3
            if( array(i)<=array(j) )then
              buffer = array(i)
              array(i) = array(j)
              array(j) = buffer
            end if
          end do
        end do
      end subroutine
        
      end module
      
      
      PROGRAM piyo
      use gcdMod
      IMPLICIT NONE
      integer(16) :: k,ans=0
      integer(16) :: a,b,c, x,y,z,buffer1
      integer(16) :: array(3),table(200,200,200)=0
      
      read*,k
      
      do x = 1,k
        do y = 1,k
          do z = 1,k
            array(1) = x
            array(2) = y
            array(3) = z
            call sort( array )
            table(array(1),array(2),array(3)) = &
              & table(array(1),array(2),array(3)) + 1
          end do
        end do
      end do
      
      
      do x = 1,k
        do y = 1,k
          do z = 1,k
            
            if(table(x,y,z)/=0)then
              buffer1 = gcd(x,y)
              ans = ans + table(x,y,z) * gcd(buffer1,z)
            end if
          end do
        end do
      end do
      
      print*,ans
      
      
      
      
      END PROGRAM