program main
      implicit none
      integer(4) :: k, a, b, mab, c, mabc
      integer(8) :: cnt

      read *, k
      cnt = 0
      do a = 1, k
        do b = 1, k
          do c = 1, k
            call gcd(a,b,mab)
            call gcd(mab, c, mabc)
            cnt = cnt + mabc
          end do
        end do
      end do
      print *, cnt

      contains

      subroutine gcd(m,n,j)
        implicit none
        integer, intent(in) :: m, n
        integer, intent(out) :: j
        integer :: i, l, x, y

        x = m
        y = n
        do 
          l = mod(x,y)
          if ( l == 0 ) exit
          x = y
          y = l
        end do
        j = y

      end subroutine gcd

end program main
