import math

from functools import reduce





def gcd(*numbers):

    return reduce(math.gcd, numbers)





def main():

    K = int(eval(input()))

    s = sum([gcd(i+1, j+1, k+1) for i in range(K) for j in range(K) for k in range(K)])



    print(s)





if __name__=="__main__":

    main()

import math





def main():

    K = int(eval(input()))

    s = 0

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

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

            tmp = math.gcd(i, j)

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

                s += math.gcd(tmp, k)



    print(s)





if __name__=="__main__":

    main()

import math
from functools import reduce


def gcd(*numbers):
    return reduce(math.gcd, numbers)


def main():
    K = int(eval(input()))
    s = sum(
        [gcd(i + 1, j + 1, k + 1) for i in range(K) for j in range(K) for k in range(K)]
    )
    print(s)


if __name__ == "__main__":
    main()

import math


def main():
    K = int(eval(input()))
    s = 0
    for i in range(1, K + 1):
        for j in range(1, K + 1):
            tmp = math.gcd(i, j)
            for k in range(1, K + 1):
                s += math.gcd(tmp, k)
    print(s)


if __name__ == "__main__":
    main()

-from functools import reduce - - -def gcd(*numbers): -    return reduce(math.gcd, numbers) -    s = sum( -        [gcd(i + 1, j + 1, k + 1) for i in range(K) for j in range(K) for k in range(K)] -    ) +    s = 0 +    for i in range(1, K + 1): +        for j in range(1, K + 1): +            tmp = math.gcd(i, j) +            for k in range(1, K + 1): +                s += math.gcd(tmp, k)