#include <vector>
#include <string>
#include <iostream>
#include <queue>
#include <algorithm>
#include <functional>
#include <map>
#include <array>
#include <cmath>

using namespace std;

int gcd(int a, int b){
    if(a < b){
        return gcd(b, a);
    }

    int r = a % b;
    while(r != 0){
        a = b;
        b = r;
        r = a % b;
    }

    return b;
}

int gcd3(int a, int b, int c){
    int ab = gcd(a, b);
    return gcd(ab, c);
}


int main(int argc, char* argv[]){

    long long N;
    cin >> N;

    long long sum = 0;

    for(int a = 1; a <= N; ++a){
        for(int b = 1; b <= N; ++b){
            for(int c = 1; c <= N; ++c){
                sum += gcd3(a, b, c);
            }
        }
    }

    cout << sum;
}

