program ABC162_C
  integer::G,A,H,I,J,K,L
  read(5,*)K
  A=0
  do I=1,K
     do J=1,K
        do L=1,K
           call gcd(J,L,G)
           call gcd(I,G,H)
           A=A+H
        end do
     end do
  end do
  write(6,*)A
end program ABC162_C

  subroutine gcd(A,B,G)
    integer::A,B,C,G,M
    if(A<B) then
       D=B
       E=A
    else
       D=A
       E=B
    end if
    do while(E/=0)
       M=mod(D,E)
       D=E
       E=M
    end do
    G=D
  end subroutine gcd