from functools import lru_cache





@lru_cache(maxsize=None)

def gcd(a, b):

    if a < b:

        a, b = b, a

    if a % b == 0:

        return b

    return gcd(b, a % b)





def main():

    ans = 0

    k = int(eval(input()))

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

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

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

                ans += gcd(gcd(i, j), l)

    print(ans)





if __name__ == '__main__':

    main()

from math import gcd





def main():

    ans = 0

    k = int(eval(input()))

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

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

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

                ans += gcd(gcd(i, j), l)

    print(ans)





if __name__ == '__main__':

    main()

from functools import lru_cache


@lru_cache(maxsize=None)
def gcd(a, b):
    if a < b:
        a, b = b, a
    if a % b == 0:
        return b
    return gcd(b, a % b)


def main():
    ans = 0
    k = int(eval(input()))
    for i in range(1, k + 1):
        for j in range(1, k + 1):
            for l in range(1, k + 1):
                ans += gcd(gcd(i, j), l)
    print(ans)


if __name__ == "__main__":
    main()

from math import gcd


def main():
    ans = 0
    k = int(eval(input()))
    for i in range(1, k + 1):
        for j in range(1, k + 1):
            for l in range(1, k + 1):
                ans += gcd(gcd(i, j), l)
    print(ans)


if __name__ == "__main__":
    main()

-from functools import lru_cache - - -@lru_cache(maxsize=None) -def gcd(a, b): -    if a < b: -        a, b = b, a -    if a % b == 0: -        return b -    return gcd(b, a % b) +from math import gcd