#!/usr/bin python3

# -*- coding: utf-8 -*-



import sys

input = sys.stdin.readline

from math import gcd



def main():

    K=int(eval(input()))

    ret = 0

    for a in range(K):

        for b in range(K):

            for c in range(K):

                ret += gcd(gcd(a+1,b+1),c+1)

    print(ret)



if __name__ == '__main__':

    main()
#!/usr/bin python3

# -*- coding: utf-8 -*-



import sys

input = sys.stdin.readline

from math import gcd





def main():

    K=int(eval(input()))

    ret = 0

    gc=[[-1]*K for _ in range(K)]

    for a in range(K):

        for b in range(K):

            if gc[a][b]==-1:

                gc[a][b]=gcd(a+1,b+1)

                gc[b][a]=gc[a][b]

            x=gc[a][b]

            for c in range(K):

                if gc[x-1][c]==-1:

                    gc[x-1][c]=gcd(x,c+1)

                    gc[c][x-1]=gc[x-1][c]

                ret += gc[x-1][c]

    print(ret)



if __name__ == '__main__':

    main()
#!/usr/bin python3
# -*- coding: utf-8 -*-
import sys

input = sys.stdin.readline
from math import gcd


def main():
    K = int(eval(input()))
    ret = 0
    for a in range(K):
        for b in range(K):
            for c in range(K):
                ret += gcd(gcd(a + 1, b + 1), c + 1)
    print(ret)


if __name__ == "__main__":
    main()

#!/usr/bin python3
# -*- coding: utf-8 -*-
import sys

input = sys.stdin.readline
from math import gcd


def main():
    K = int(eval(input()))
    ret = 0
    gc = [[-1] * K for _ in range(K)]
    for a in range(K):
        for b in range(K):
            if gc[a][b] == -1:
                gc[a][b] = gcd(a + 1, b + 1)
                gc[b][a] = gc[a][b]
            x = gc[a][b]
            for c in range(K):
                if gc[x - 1][c] == -1:
                    gc[x - 1][c] = gcd(x, c + 1)
                    gc[c][x - 1] = gc[x - 1][c]
                ret += gc[x - 1][c]
    print(ret)


if __name__ == "__main__":
    main()

+    gc = [[-1] * K for _ in range(K)] +            if gc[a][b] == -1: +                gc[a][b] = gcd(a + 1, b + 1) +                gc[b][a] = gc[a][b] +            x = gc[a][b] -                ret += gcd(gcd(a + 1, b + 1), c + 1) +                if gc[x - 1][c] == -1: +                    gc[x - 1][c] = gcd(x, c + 1) +                    gc[c][x - 1] = gc[x - 1][c] +                ret += gc[x - 1][c]