program main
  implicit none

  integer :: n, i, j, k, s

  read *, n

  s = 0
  do i = 1, n
     do j = i, n
        do k = j, n
           if (i == j) then
              if (j == k) then
                 s = s + gcd(i,j,k)
              else
                 s = s + 3*gcd(i,j,k)
              end if
           else
              if (j == k) then
                 s = s + 3*gcd(i,j,k)
              else
                 s = s + 6*gcd(i,j,k)
              end if
           end if
        end do
     end do
  end do

  print *, s
  

  stop
contains

  integer function gcd(i,j,k)
    implicit none
    integer, intent(in) :: i, j, k
    integer :: a, b, c, d, e
    logical :: log

    a = i
    b = j
    c = k
    if (a < b) then
       d = a
       a = b
       b = d
    end if

    log = .true.
    do while(log .eqv. .true.)
       if (b == 0) then
          d = a
          log = .false.
          exit
       else
          d = mod(a,b)
          a = b
          b = d
       end if
    end do

    if (c < d) then
       e = c
       c = d
       d = e
    end if

    log = .true.
    do while(log .eqv. .true.)
       if (d == 0) then
          gcd = c
          log = .false.
          exit
       else
          e = mod(c,d)
          c = d
          d = e
       end if
    end do

    return
  end function gcd
    
end program main
