import std.conv, std.stdio;
import std.algorithm, std.array, std.range, std.string;
import std.functional;
void main()
{
    auto n = readln.chomp.to!int;
    long answer;
    foreach (a; 1..n+1)
        foreach (b; 1..n+1)
        {
            immutable g = gcd(a, b);
            foreach (c; 1..n+1)
                answer += gcd(g, c);
        }
    answer.writeln;
}
alias gcd = memoize!_gcd;

long _gcd(in long a, in long b)
{
    if (a == 0) return b;
    if (b == 0) return a;
    if (a & b & 1) return gcd(max(a, b) - min(a, b), min(a, b));
    if (!((a | b) & 1)) return gcd(a >> 1, b >> 1) << 1;
    if (a & 1) return gcd(a, b >> 1);
    if (b & 1) return gcd(a >> 1, b);
    assert (false);
}

unittest
{
    assert (gcd(0, 0) == 0);
    assert (gcd(0, 1) == 1);
    assert (gcd(0, 2) == 2);
    assert (gcd(1, 0) == 1);
    assert (gcd(1, 1) == 1);
    assert (gcd(1, 2) == 1);
    assert (gcd(2, 0) == 2);
    assert (gcd(2, 1) == 1);
    assert (gcd(2, 2) == 2);
    assert (gcd(20, 30) == 10);
    assert (gcd(16, 24) == 8);
}