local K = io.read("*n")

local function euc(arg1, arg2)
    --print(arg1, arg2)
    if arg1 == arg2 or arg2 == 0 then
        return arg1
    elseif arg1 == 0 then
        return arg2
    elseif arg1 > arg2 then
        return euc(arg2, arg1 % arg2)
    else
        return euc(arg1, arg2 % arg1)
    end
end

local function gcd(i, j, k)
    return euc(euc(i, j), euc(j, k))
end

local total = 0
for i = 1, K do
    for j = 1, K do
        for k = 1, K do
            total = total + gcd(i, j, k)
        end
    end
end

print(total)