import math



def main():

    n = int(eval(input()))

    ans = 0

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

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

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

                tmp = math.gcd(i,j)

                ans += math.gcd(tmp,k)



    print(ans)



if __name__ == "__main__":

    main()
import math



def main():

    n = int(eval(input()))

    ans = 0

    a = {}

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

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

            tmp = math.gcd(i,j)

            a[tmp] = a.get(tmp, 0)

            a[tmp] += 1





    for key, val in list(a.items()):

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

            ans += math.gcd(key,j) * val



    print(ans)



if __name__ == "__main__":

    main()

import math


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


if __name__ == "__main__":
    main()

import math


def main():
    n = int(eval(input()))
    ans = 0
    a = {}
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            tmp = math.gcd(i, j)
            a[tmp] = a.get(tmp, 0)
            a[tmp] += 1
    for key, val in list(a.items()):
        for j in range(1, n + 1):
            ans += math.gcd(key, j) * val
    print(ans)


if __name__ == "__main__":
    main()

+    a = {} -            for k in range(1, n + 1): -                tmp = math.gcd(i, j) -                ans += math.gcd(tmp, k) +            tmp = math.gcd(i, j) +            a[tmp] = a.get(tmp, 0) +            a[tmp] += 1 +    for key, val in list(a.items()): +        for j in range(1, n + 1): +            ans += math.gcd(key, j) * val