#!/usr/bin/env python3

import sys

import math



def solve(K: int):

    answer = 0

    GCDLIST = [[1]*(K+1) for _ in range(K+1)]



    for i in range(1,K+1):

        for j in range(1,K+1):

            g = math.gcd(i,j)

            GCDLIST[i][j] = g

            GCDLIST[j][i] = g

    

    for i in range(1,K+1):

        for j in range(1,K+1): 

            for k in range(1,K+1):

                answer += GCDLIST[GCDLIST[i][j]][k]

    

    print(answer)

    return





# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)

def main():

    def iterate_tokens():

        for line in sys.stdin:

            for word in line.split():

                yield word

    tokens = iterate_tokens()

    K = int(next(tokens))  # type: int

    solve(K)



if __name__ == '__main__':

    main()

#!/usr/bin/env python3

import sys

import math



def solve(K: int):

    answer = 0

    # GCDLIST = [[1]*(K+1) for _ in range(K+1)]



    # for i in range(1,K+1):

    #     for j in range(1,K+1):

    #         g = math.gcd(i,j)

    #         GCDLIST[i][j] = g

    #         GCDLIST[j][i] = g

    

    for i in range(1,K+1):

        for j in range(1,K+1): 

            for k in range(1,K+1):

                answer += math.gcd(math.gcd(i,j),k)

    

    print(answer)

    return





# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)

def main():

    def iterate_tokens():

        for line in sys.stdin:

            for word in line.split():

                yield word

    tokens = iterate_tokens()

    K = int(next(tokens))  # type: int

    solve(K)



if __name__ == '__main__':

    main()

#!/usr/bin/env python3
import sys
import math


def solve(K: int):
    answer = 0
    GCDLIST = [[1] * (K + 1) for _ in range(K + 1)]
    for i in range(1, K + 1):
        for j in range(1, K + 1):
            g = math.gcd(i, j)
            GCDLIST[i][j] = g
            GCDLIST[j][i] = g
    for i in range(1, K + 1):
        for j in range(1, K + 1):
            for k in range(1, K + 1):
                answer += GCDLIST[GCDLIST[i][j]][k]
    print(answer)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    K = int(next(tokens))  # type: int
    solve(K)


if __name__ == "__main__":
    main()

#!/usr/bin/env python3
import sys
import math


def solve(K: int):
    answer = 0
    # GCDLIST = [[1]*(K+1) for _ in range(K+1)]
    # for i in range(1,K+1):
    #     for j in range(1,K+1):
    #         g = math.gcd(i,j)
    #         GCDLIST[i][j] = g
    #         GCDLIST[j][i] = g
    for i in range(1, K + 1):
        for j in range(1, K + 1):
            for k in range(1, K + 1):
                answer += math.gcd(math.gcd(i, j), k)
    print(answer)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    K = int(next(tokens))  # type: int
    solve(K)


if __name__ == "__main__":
    main()

-    GCDLIST = [[1] * (K + 1) for _ in range(K + 1)] -    for i in range(1, K + 1): -        for j in range(1, K + 1): -            g = math.gcd(i, j) -            GCDLIST[i][j] = g -            GCDLIST[j][i] = g +    # GCDLIST = [[1]*(K+1) for _ in range(K+1)] +    # for i in range(1,K+1): +    #     for j in range(1,K+1): +    #         g = math.gcd(i,j) +    #         GCDLIST[i][j] = g +    #         GCDLIST[j][i] = g -                answer += GCDLIST[GCDLIST[i][j]][k] +                answer += math.gcd(math.gcd(i, j), k)