import sys

import os

import math



ii = lambda: int(sys.stdin.buffer.readline().rstrip())

il = lambda: list(map(int, sys.stdin.buffer.readline().split()))

fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))

iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]



iss = lambda: sys.stdin.buffer.readline().decode().rstrip()

sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))

isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]



MOD = 10 ** 9 + 7





def fb(i):

    if i % 15 == 0 or i % 3 == 0 or i % 5 == 0:

        return True

    else:

        return False





def main():

    if os.getenv("LOCAL"):

        sys.stdin = open("input.txt", "r")



    N = ii()

    ret = 0

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

        for m in range(1, N + 1):

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

                ret += math.gcd(math.gcd(n,m),l)

    print(ret)





if __name__ == '__main__':

    main()

import sys

import os

import math



ii = lambda: int(sys.stdin.buffer.readline().rstrip())

il = lambda: list(map(int, sys.stdin.buffer.readline().split()))

fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))

iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]



iss = lambda: sys.stdin.buffer.readline().decode().rstrip()

sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))

isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]



MOD = 10 ** 9 + 7





def main():

    if os.getenv("LOCAL"):

        sys.stdin = open("input.txt", "r")



    N = ii()

    ret = 0

    for a in range(1, N + 1):

        for b in range(1, N + 1):

            q = math.gcd(a, b)

            for c in range(1, N + 1):

                ret += math.gcd(q, c)

    print(ret)





if __name__ == '__main__':

    main()

import sys
import os
import math

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]
iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]
MOD = 10**9 + 7


def fb(i):
    if i % 15 == 0 or i % 3 == 0 or i % 5 == 0:
        return True
    else:
        return False


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")
    N = ii()
    ret = 0
    for n in range(1, N + 1):
        for m in range(1, N + 1):
            for l in range(1, N + 1):
                ret += math.gcd(math.gcd(n, m), l)
    print(ret)


if __name__ == "__main__":
    main()

import sys
import os
import math

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]
iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]
MOD = 10**9 + 7


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")
    N = ii()
    ret = 0
    for a in range(1, N + 1):
        for b in range(1, N + 1):
            q = math.gcd(a, b)
            for c in range(1, N + 1):
                ret += math.gcd(q, c)
    print(ret)


if __name__ == "__main__":
    main()

-def fb(i): -    if i % 15 == 0 or i % 3 == 0 or i % 5 == 0: -        return True -    else: -        return False - - -    for n in range(1, N + 1): -        for m in range(1, N + 1): -            for l in range(1, N + 1): -                ret += math.gcd(math.gcd(n, m), l) +    for a in range(1, N + 1): +        for b in range(1, N + 1): +            q = math.gcd(a, b) +            for c in range(1, N + 1): +                ret += math.gcd(q, c)