def gcd(a,b):

	while(b):

		a,b = b,a%b

	return a

k = int(eval(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(a,gcd(b,c))



print(ans)
def gcd(a,b):

	while(b):

		a,b = b,a%b

	return a

def main():

	k = int(eval(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(a,gcd(b,c))



	print(ans)

if __name__ == '__main__':

	main()
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


k = int(eval(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(a, gcd(b, c))
print(ans)

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def main():
    k = int(eval(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(a, gcd(b, c))
    print(ans)


if __name__ == "__main__":
    main()

-k = int(eval(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(a, gcd(b, c)) -print(ans) +def main(): +    k = int(eval(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(a, gcd(b, c)) +    print(ans) + + +if __name__ == "__main__": +    main()