def gcd(a, b):

    if a < b:

        return gcd(b, a)

    d = a % b

    if d == 0:

        return b

    return gcd(b, d)



def main():

    K = int(eval(input()))

    table = [[0]*K for i in range(K)]

    for i in range(K):

        for j in range(K):

            table[i][j] = gcd(i + 1, j + 1)

    ans = 0

    for i in range(K):

        for j in range(K):

            for k in range(K):

                ans += gcd(i + 1, table[j][k])

    print(ans)



if __name__ == '__main__':

    main()

def gcd(a, b):

    if a < b:

        return gcd(b, a)

    d = a % b

    if d == 0:

        return b

    return gcd(b, d)



def main():

    K = int(eval(input()))

    table = [[0]*K for i in range(K)]

    for i in range(K):

        for j in range(K):

            table[i][j] = gcd(i + 1, j + 1)

    ans = 0

    for i in range(K):

        for j in range(K):

            for k in range(K):

                ans += table[i][table[j][k]-1]

    print(ans)



if __name__ == '__main__':

    main()

def gcd(a, b):
    if a < b:
        return gcd(b, a)
    d = a % b
    if d == 0:
        return b
    return gcd(b, d)


def main():
    K = int(eval(input()))
    table = [[0] * K for i in range(K)]
    for i in range(K):
        for j in range(K):
            table[i][j] = gcd(i + 1, j + 1)
    ans = 0
    for i in range(K):
        for j in range(K):
            for k in range(K):
                ans += gcd(i + 1, table[j][k])
    print(ans)


if __name__ == "__main__":
    main()

def gcd(a, b):
    if a < b:
        return gcd(b, a)
    d = a % b
    if d == 0:
        return b
    return gcd(b, d)


def main():
    K = int(eval(input()))
    table = [[0] * K for i in range(K)]
    for i in range(K):
        for j in range(K):
            table[i][j] = gcd(i + 1, j + 1)
    ans = 0
    for i in range(K):
        for j in range(K):
            for k in range(K):
                ans += table[i][table[j][k] - 1]
    print(ans)


if __name__ == "__main__":
    main()

-                ans += gcd(i + 1, table[j][k]) +                ans += table[i][table[j][k] - 1]