n=int(eval(input()))

# a,b,c=map(int,input().split())

cnt=0

def gcd(a,b):

    if a==0: return b

    else: return gcd(b%a,a)

# print(gcd(a,b))



def gcd_3(a,b,c):

    if gcd(a,b)==0: return c

    else: return gcd(c % gcd(a,b),gcd(a,b))



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

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

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

            # print(i,j,k)

            cnt+=gcd_3(i,j,k)

print(cnt)

def main():

    from math import gcd



    K = int(eval(input()))



    result = 0

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

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

            t = gcd(a, b)

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

                result += gcd(t, c)

    print(result)





main()
n = int(eval(input()))
# a,b,c=map(int,input().split())
cnt = 0


def gcd(a, b):
    if a == 0:
        return b
    else:
        return gcd(b % a, a)


# print(gcd(a,b))
def gcd_3(a, b, c):
    if gcd(a, b) == 0:
        return c
    else:
        return gcd(c % gcd(a, b), gcd(a, b))


for i in range(1, n + 1):
    for j in range(1, n + 1):
        for k in range(1, n + 1):
            # print(i,j,k)
            cnt += gcd_3(i, j, k)
print(cnt)

def main():
    from math import gcd

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


main()

-n = int(eval(input())) -# a,b,c=map(int,input().split()) -cnt = 0 +def main(): +    from math import gcd + +    K = int(eval(input())) +    result = 0 +    for a in range(1, K + 1): +        for b in range(1, K + 1): +            t = gcd(a, b) +            for c in range(1, K + 1): +                result += gcd(t, c) +    print(result) -def gcd(a, b): -    if a == 0: -        return b -    else: -        return gcd(b % a, a) - - -# print(gcd(a,b)) -def gcd_3(a, b, c): -    if gcd(a, b) == 0: -        return c -    else: -        return gcd(c % gcd(a, b), gcd(a, b)) - - -for i in range(1, n + 1): -    for j in range(1, n + 1): -        for k in range(1, n + 1): -            # print(i,j,k) -            cnt += gcd_3(i, j, k) -print(cnt) +main()