program test
implicit none
integer(8) :: i,j,k,a,b,c,ans=0

read(*,*) k

do a=1,k
  do b=1,k
    do c=1,k
      ans = ans + gcd(gcd(a,b),c)
    enddo
  enddo
enddo

write(*,*) ans

contains
integer(8) function gcd(a,b)
  implicit none
  integer(8), intent(in) :: a,b
  integer(8) :: l,m,n

  m = a
  n = b

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

  gcd = n

  return
end function gcd

end program