#!/usr/bin/env python3

# -*- coding: utf-8 -*-



import math

from functools import lru_cache





@lru_cache(maxsize=None)

def get_gcd(a, b):

    return math.gcd(a, b)





def main():

    k = int(input().strip())

    sum = 0

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

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

            a_, b_ = (a, b) if a < b else (b, a)

            a_b_ = get_gcd(a_, b_)

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

                sum += get_gcd(a_b_, c) if a_b_ < c else get_gcd(c, a_b_)

    print(sum)





if __name__ == '__main__':

    main()

#!/usr/bin/env python3

# -*- coding: utf-8 -*-



import math

from functools import lru_cache



# prime_200 = {

#     2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,

#     73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173,

#     179, 181, 191, 193, 197, 199

# }



@lru_cache(maxsize=None)

def get_gcd(a, b):

    return math.gcd(a, b)





def main():

    k = int(input().strip())

    sum = 0



    # すべて同じ(a, a, a)

    sum += k * (k + 1) // 2



    # 2 つ同じ（a, a, b）

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

        # if a in prime_200:

        #     sum += 3 * (k - 1)  # 自分と同じを除く分 * 3c1

        #     continue

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

            if a == b:

                continue  # 同じは除く

            # if b in prime_200:

            #     sum += 3  # b が素数なら gcd は 1 * 3c1

            #     continue

            a_, b_ = (a, b) if a < b else (b, a)

            sum += 3 * get_gcd(a_, b_)



    # 3 つ違う (a, b, c)

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

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

            if a == b:

                continue

            a_b_ = get_gcd(a, b)

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

                if a == c or b == c:

                    continue

                sum += 6 * get_gcd(a_b_, c)

    print(sum)





if __name__ == '__main__':

    main()

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import math
from functools import lru_cache


@lru_cache(maxsize=None)
def get_gcd(a, b):
    return math.gcd(a, b)


def main():
    k = int(input().strip())
    sum = 0
    for a in range(1, k + 1):
        for b in range(1, k + 1):
            a_, b_ = (a, b) if a < b else (b, a)
            a_b_ = get_gcd(a_, b_)
            for c in range(1, k + 1):
                sum += get_gcd(a_b_, c) if a_b_ < c else get_gcd(c, a_b_)
    print(sum)


if __name__ == "__main__":
    main()

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import math
from functools import lru_cache

# prime_200 = {
#     2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
#     73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
#     179, 181, 191, 193, 197, 199
# }
@lru_cache(maxsize=None)
def get_gcd(a, b):
    return math.gcd(a, b)


def main():
    k = int(input().strip())
    sum = 0
    # すべて同じ(a, a, a)
    sum += k * (k + 1) // 2
    # 2 つ同じ（a, a, b）
    for a in range(1, k + 1):
        # if a in prime_200:
        #     sum += 3 * (k - 1)  # 自分と同じを除く分 * 3c1
        #     continue
        for b in range(1, k + 1):
            if a == b:
                continue  # 同じは除く
            # if b in prime_200:
            #     sum += 3  # b が素数なら gcd は 1 * 3c1
            #     continue
            a_, b_ = (a, b) if a < b else (b, a)
            sum += 3 * get_gcd(a_, b_)
    # 3 つ違う (a, b, c)
    for a in range(1, k + 1):
        for b in range(a + 1, k + 1):
            if a == b:
                continue
            a_b_ = get_gcd(a, b)
            for c in range(b + 1, k + 1):
                if a == c or b == c:
                    continue
                sum += 6 * get_gcd(a_b_, c)
    print(sum)


if __name__ == "__main__":
    main()

- +# prime_200 = { +#     2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, +#     73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, +#     179, 181, 191, 193, 197, 199 +# } +    # すべて同じ(a, a, a) +    sum += k * (k + 1) // 2 +    # 2 つ同じ（a, a, b） +        # if a in prime_200: +        #     sum += 3 * (k - 1)  # 自分と同じを除く分 * 3c1 +        #     continue +            if a == b: +                continue  # 同じは除く +            # if b in prime_200: +            #     sum += 3  # b が素数なら gcd は 1 * 3c1 +            #     continue -            a_b_ = get_gcd(a_, b_) -            for c in range(1, k + 1): -                sum += get_gcd(a_b_, c) if a_b_ < c else get_gcd(c, a_b_) +            sum += 3 * get_gcd(a_, b_) +    # 3 つ違う (a, b, c) +    for a in range(1, k + 1): +        for b in range(a + 1, k + 1): +            if a == b: +                continue +            a_b_ = get_gcd(a, b) +            for c in range(b + 1, k + 1): +                if a == c or b == c: +                    continue +                sum += 6 * get_gcd(a_b_, c)