{-# LANGUAGE FlexibleInstances    #-}
{-# LANGUAGE GADTs                #-}
{-# LANGUAGE OverloadedStrings    #-}
{-# LANGUAGE RankNTypes           #-}
{-# LANGUAGE ScopedTypeVariables  #-}
{-# LANGUAGE Strict               #-}
{-# LANGUAGE TypeApplications     #-}
{-# LANGUAGE TypeSynonymInstances #-}

import           Data.ByteString.Char8 (ByteString, putStrLn)
import qualified Data.ByteString.Char8 as BS
import           Prelude               hiding (getLine, print, putStrLn, readLn,
                                        unwords, words)

import           Control.Monad         (replicateM)
import           Data.List             (sort)
import           Data.List.Extra       (nubOrd)
import           Data.Maybe            (isJust)
import           Prelude               (print)

class (Read a, Show a) =>
      CPString a
  where
  cpRead :: String -> a
  cpShow :: a -> ByteString
  cpRead = read
  cpShow = BS.pack . show

instance CPString ByteString where
  cpRead = BS.pack
  cpShow = id

instance CPString String where
  cpRead = id
  cpShow = BS.pack

instance CPString Char where
  cpRead [a] = a
  cpShow = BS.pack . pure

instance CPString Int

instance CPString Integer

instance CPString Float

instance CPString Double

instance CPString Rational

instance CPString a => CPString [a]

readLine :: CPString a => IO [a]
readLine = fmap (cpRead . BS.unpack) . BS.words <$> BS.getLine

printLn :: CPString a => a -> IO ()
printLn = putStrLn . cpShow

main :: IO ()
main = do
  [k] <- readLine @Int
  printLn $ solve k

solve :: Int -> Int
solve k = sum [gcd3 a b c | a <- [1 .. k], b <- [1 .. k], c <- [1 .. k]]

gcd3 :: Int -> Int -> Int -> Int
gcd3 a b c = gcd (gcd a b) c
