   program abc162c
      implicit none
      integer::k,a,b,c,x,y,sum0
      interface
         function gcd(a,b)
            implicit none
            integer::gcd
            integer,intent(in)::a,b
         end function gcd
      end interface

         read *,k

         do a=1,k
            do b=1,k
               x=gcd(a,b)
               do c=1,k
                  y=gcd(x,c)
                  sum0=sum0+y
               end do
            end do
         end do

         print *,sum0
         stop

   end program abc162c


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

         l=a
         m=b
         n=mod(l,m)
         do while (n/=0)
            l=m
            m=n
            n=mod(l,m)
         end do
         gcd=m
         return
   end function gcd