#!/usr/bin/env python3



import sys

import pprint

from math import gcd





sys.setrecursionlimit(10 ** 6)



class Logger:

	def __init__(self, debug):

		self.debug = debug

	def print(self, *args):

		if self.debug:

			pprint.pprint(args)





def main():

	log = Logger(1)

	k = int(sys.stdin.readline())

	s = 0

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

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

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

				s += gcd(gcd(a, b), c)

	print(s)

	





main()

#!/usr/bin/env pypy3



import sys

import pprint

from math import gcd





sys.setrecursionlimit(10 ** 6)



class Logger:

	def __init__(self, debug):

		self.debug = debug

	def print(self, *args):

		if self.debug:

			pprint.pprint(args)





def main():

	log = Logger(1)

	k = int(sys.stdin.readline())

	s = 0

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

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

			x = gcd(a, b)

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

				s += gcd(x, c)

	print(s)

	





main()

#!/usr/bin/env python3
import sys
import pprint
from math import gcd

sys.setrecursionlimit(10**6)


class Logger:
    def __init__(self, debug):
        self.debug = debug

    def print(self, *args):
        if self.debug:
            pprint.pprint(args)


def main():
    log = Logger(1)
    k = int(sys.stdin.readline())
    s = 0
    for a in range(1, k + 1):
        for b in range(1, k + 1):
            for c in range(1, k + 1):
                s += gcd(gcd(a, b), c)
    print(s)


main()

#!/usr/bin/env pypy3
import sys
import pprint
from math import gcd

sys.setrecursionlimit(10**6)


class Logger:
    def __init__(self, debug):
        self.debug = debug

    def print(self, *args):
        if self.debug:
            pprint.pprint(args)


def main():
    log = Logger(1)
    k = int(sys.stdin.readline())
    s = 0
    for a in range(1, k + 1):
        for b in range(1, k + 1):
            x = gcd(a, b)
            for c in range(1, k + 1):
                s += gcd(x, c)
    print(s)


main()

-#!/usr/bin/env python3 +#!/usr/bin/env pypy3 +            x = gcd(a, b) -                s += gcd(gcd(a, b), c) +                s += gcd(x, c)