### BUGGY CODE ###

import sys
import math

from collections import defaultdict
from collections import deque
def load(vtype=int):
    return vtype(input().strip())
def load_list(seplator=" ", vtype=int):
    return [vtype(v) for v in input().strip().split(seplator)]
def exit():
    import sys
    sys.exit(0)
def perm_sub(li, used):
    if len(li) == len(used):
        return [deque()]
    k = []
    for i in range(len(li)):
        if i in used:
            continue
        used.add(i)
        sub_list = perm_sub(li, used)
        for sub in sub_list:
            sub.appendleft(li[i])
        k.extend(sub_list)
        used.discard(i)
    return k
def perm_li(li):
    return perm_sub(li, set())
def perm_n(n):
    return perm_sub(list(range(n)), set())
def join_i(li, sep=""):
    return sep.join([str(e) for e in li])
def li2n(li):
    n, base = 0, 1
    for i in range(len(li)-1, -1, -1):
        n += li[i] * base
        base *= 10
    return n
def sli2ili(li):
    return [int(s) for s in li] 
def prime_list(n):
    li = [i for i in range(2, n+1)]
    for i in range(len(li)):
        if li[i] >= int(math.sqrt(n)):
            break
        if li[i] == -1:
            continue
        for j in range(i+1, len(li)):
            if li[j] % li[i] == 0:
                li[j] = -1
    return [n for n in li if n != -1]
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while True:
        r = a % b
        if r == 0: return b
        a, b = b, r
def lcm(a, b):
    return int(a * b / gcd(a,b))
def all_subset(li):
    s = []
    n = len(li)
    for bit in range(0, (1<<(n+1))):
        ss = set()
        for i in range(0, n):
            if (bit & (1 << i)):
                ss.add(li[i])
        s.append(ss)
    return s
def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n-1)
def mCn(m, n):
    def factorial_ntimes(m, n):
        r = 1
        while n:
            r *= m
            m -= 1
            n -= 1
        return r
    return int(factorial_ntimes(m, n) / factorial(n))




d = dict()
d2 = dict()

def check(a, b, d2):
    t = tuple(sorted([a,b]))
    if t in d2:
        return d2[t]
    return False

k = load()
s = 0
for i in range(1,k+1):
    for j in range(1,k+1):
        for k in range(1, k+1):
            r = check(i, j, d2)
            target = None
            if r:
                target = k
            else:
                r = check(j, k, d2)
                if r:
                    target = i
                else:
                    r = check(i, k, d2)
                    if r:
                        target = j
                    else:
                        # print(i,j,k)
                        d2[(i,j)] = gcd(i,j)
                        r = d2[(i,j)]
                        target = k
            r2 = check(r, target, d2)
            if r2:
                s += r2
            else:
                d2[(r, target)] = gcd(r, target)
                s += d2[(r, target)]

print(s)
                

### FIXED CODE ###

from math import gcd
 
def main():
    k = int(input())
    ans = 0
 
    for a in range(1, k+1):
        for b in range(1, k+1):
            for c in range(1, k+1):
                ans += gcd(gcd(a, b), c)
 
    print(ans)
 
if __name__ == "__main__":
    main()