# abc162_c.py
# https://atcoder.jp/contests/abc162/tasks/abc162_c

# C - Sum of gcd of Tuples (Easy) /
# 実行時間制限: 2 sec / メモリ制限: 1024 MB
# 配点 : 300点

# 問題文
# K∑a=1K∑b=1K∑c=1gcd(a,b,c)を求めてください。
# ただし、gcd(a,b,c)は a,b,cの最大公約数を表します。

# 制約
#     1≤K≤200
#     Kは整数

# 入力
# 入力は以下の形式で標準入力から与えられる。
# K

# 出力
# K∑a=1K∑b=1K∑c=1gcd(a,b,c)の値を出力せよ。

# 入力例 1
# 2

# 出力例 1
# 9

# gcd(1,1,1)+gcd(1,1,2)+gcd(1,2,1)+gcd(1,2,2)+gcd(2,1,1)+gcd(2,1,2)+gcd(2,2,1)+gcd(2,2,2)
#  =1+1+1+1+1+1+1+2=9
# となるため、答えは 9です。

# 入力例 2
# 200

# 出力例 2
# 10813692


global FLAG_LOG
FLAG_LOG = False


def log(value):
    # FLAG_LOG = True
    # FLAG_LOG = False
    if FLAG_LOG:
        print(str(value))


def calculation(lines):
    # S = lines[0]
    K = int(lines[0])
    # N, M = list(map(int, lines[0].split()))
    # values = list(map(int, lines[1].split()))
    # values = list(map(int, lines[2].split()))
    # values = list()
    # for i in range(N):
    #     values.append(int(lines[i]))
    # valueses = list()
    # for i in range(N):
    #     valueses.append(list(map(int, lines[i+1].split())))

    import math

    dic = dict()
    for x in range(1, K):
        for y in range(x+1, K+1):
            dic[(x, y)] = math.gcd(x, y)

    su = 0
    for a in range(1, K+1):
        for b in range(1, K+1):
            for c in range(1, K+1):
                # ☆これだと5秒かかる
                # su += get_gcd(a, b, c)
                tmp = [a, b, c]
                tmp.sort()
                x, y, z = tmp
                # けど...速くしきらんかった...
                su += get_gcd_by_dic(dic, x, y, z)

    return [su]


def gcd(p, q):
    if p % q == 0:
        return q
    else:
        return gcd(q, p % q)


def calculation2(lines):
    K = int(lines[0])

    gcdlist = [[0 for i in range(K)] for j in range(K)]
    # print(gcdlist)

    for i in range(K):
        for j in range(i, K):
            tmp = gcd(i+1, j+1)
            gcdlist[i][j] = tmp
            gcdlist[j][i] = tmp
    # print(gcdlist)

    su = 0
    for a in range(K):
        for b in range(K):
            for c in range(K):
                if a == b == c:
                    tmp = a+1
                elif a == b:
                    tmp = gcdlist[a][c]
                elif a == c:
                    tmp = gcdlist[a][b]
                elif b == c:
                    tmp = gcdlist[a][b]
                else:
                    tmp = gcdlist[a][gcdlist[b][c]-1]
                # print(f'a=[{a}], b=[{b}], c=[{c}], tmp=[{tmp}]')
                su += tmp

    return [su]


def calculation3(lines):
    K = int(lines[0])
    su = 0
    for a in range(K):
        for b in range(K):
            for c in range(K):
                su += gcd(gcd(a+1, b+1), c+1)

    return [su]


# def get_gcd(a, b, c):
#     import math
#     return math.gcd(math.gcd(a, b), math.gcd(a, c))


def get_gcd_by_dic(dic, a, b, c):
    if a == b:
        x = a
    else:
        x = dic[(a, b)]
    if a == c:
        y = a
    else:
        y = dic[(a, c)]
    if x == y:
        ret = x
    elif x > y:
        ret = dic[(y, x)]
    else:
        ret = dic[(x, y)]
    return ret


# 引数を取得
def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines


# テストデータ
def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['2']
        lines_export = [9]
    if pattern == 2:
        lines_input = ['200']
        lines_export = [10813692]
    return lines_input, lines_export


# 動作モード判別
def get_mode():
    import sys
    args = sys.argv
    global FLAG_LOG
    if len(args) == 1:
        mode = 0
        FLAG_LOG = False
    else:
        mode = int(args[1])
        FLAG_LOG = True
    return mode


# 主処理
def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(1)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation2(lines_input)

    for line_result in lines_result:
        print(line_result)

    # if mode > 0:
    #     print(f'lines_input=[{lines_input}]')
    #     print(f'lines_export=[{lines_export}]')
    #     print(f'lines_result=[{lines_result}]')
    #     if lines_result == lines_export:
    #         print('OK')
    #     else:
    #         print('NG')
    # finished = time.time()
    # duration = finished - started
    # print(f'duration=[{duration}]')


# 起動処理
if __name__ == '__main__':
    main()


Measured memory_bytes: 9512