import math





def main():

    k = int(eval(input()))

    ans = 0

    gcd = math.gcd

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

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

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

                ans += gcd(gcd(i,j), l)



    print(ans)



main()
import math





def main():

    k = int(eval(input()))

    ans = 0

    gcd = math.gcd

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

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

            ij = gcd(i,j)

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

                ans += gcd(ij, l)



    print(ans)



main()

import math


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


main()

import math


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


main()

+            ij = gcd(i, j) -                ans += gcd(gcd(i, j), l) +                ans += gcd(ij, l)