program sum_of_gcd_of_tuples_easy
  implicit none
  integer(8) :: k, a, b, c, s = 0_8, g
  read(*,*) k
  do a = 1_8, k
    do b = 1_8, k
      g = gcd(a,b)
      do c = 1_8, k
        s = s+gcd(g,c)
      end do
    end do
  end do
  write(*,'(i0)') s
contains
  integer(8) function gcd(a,b)
    integer(8), intent(in) :: a, b
    integer(8) :: x, y
    x = max(abs(a),abs(b))
    y = min(abs(a),abs(b))
    do while (y > 0)
      gcd = y
      y = mod(x,y)
      x = gcd
    end do
  end
end program sum_of_gcd_of_tuples_easy