import std;

int calc(int k) {
    int sum = 0;
    foreach (a; 1..k+1) {
        foreach (b; 1..k+1) {
            foreach (c; 1..k+1) {
                sum += gcd(a, gcd(b, c));
            }
        }
    }
    return sum;
}

void main() {
    int k; scan(k);
    writeln(calc(k));
}

void scan(T...)(ref T a) {
    string[] ss = readln.split;
    foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
