import math

from functools import reduce



def gcd(*numbers):

    return reduce(math.gcd, numbers)



def main():

    K = int(eval(input()))

    ans = 0

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

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

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

              ans += gcd(i, j, k)



    print(ans)



if __name__ == "__main__":

    main()
from math import gcd



def main():

    K = int(eval(input()))

    ans = 0

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

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

            g = gcd(i, j)

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

              ans += gcd(g, k)



    print(ans)



if __name__ == "__main__":

    main()
import math
from functools import reduce


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


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


if __name__ == "__main__":
    main()

from math import gcd


def main():
    K = int(eval(input()))
    ans = 0
    for i in range(1, K + 1):
        for j in range(1, K + 1):
            g = gcd(i, j)
            for k in range(1, K + 1):
                ans += gcd(g, k)
    print(ans)


if __name__ == "__main__":
    main()

-import math -from functools import reduce - - -def gcd(*numbers): -    return reduce(math.gcd, numbers) +from math import gcd +            g = gcd(i, j) -                ans += gcd(i, j, k) +                ans += gcd(g, k)