def main():

    K = int(eval(input()))

    from math import gcd

    ans = 0

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

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

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

                d = gcd(a, b)

                ans += gcd(c, d)

    print(ans)





if __name__ == '__main__':

    main()

def main():

    K = int(eval(input()))

    from math import gcd

    ans = 0

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

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

            g = gcd(i, j)

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

                ans += gcd(g, k)

    print(ans)





if __name__ == '__main__':

    main()

def main():
    K = int(eval(input()))
    from math import gcd

    ans = 0
    for a in range(1, K + 1):
        for b in range(1, K + 1):
            for c in range(1, K + 1):
                d = gcd(a, b)
                ans += gcd(c, d)
    print(ans)


if __name__ == "__main__":
    main()

def main():
    K = int(eval(input()))
    from math import gcd

    ans = 0
    for i in range(1, K + 1):
        for j in range(1, K + 1):
            g = gcd(i, j)
            for k in range(1, K + 1):
                ans += gcd(g, k)
    print(ans)


if __name__ == "__main__":
    main()

-    for a in range(1, K + 1): -        for b in range(1, K + 1): -            for c in range(1, K + 1): -                d = gcd(a, b) -                ans += gcd(c, d) +    for i in range(1, K + 1): +        for j in range(1, K + 1): +            g = gcd(i, j) +            for k in range(1, K + 1): +                ans += gcd(g, k)