#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>

using namespace std;

long long int gcd(long long int k,int i,int j,int l)
{
    long long int a, b, c;

                a = i;
                b = j;
                while (1)
                {

                    if (a == 0)
                    {
                        c = l;
                        while (1)
                        {

                            if (b == 0)
                            {
                                return  c;
                                
                            }
                            else if (c == 0)
                            {
                                return b;
                               
                            }
                            (b >= c) ? b %= c : c %= b;
                        }
                    }
                    else if (b == 0)
                    {
                        c = l;
                        while (1)
                        {

                            if (a == 0)
                            {
                                return c;
                               
                            }
                            else if (c == 0)
                            {
                                return a;
                               
                            }
                            (a >= c) ? a %= c : c %= a;
                        }
                    }
                    (a >= b) ? a %= b : b %= a;
                }
 }

int main()
{

    long long int k, result = 0;
    cin >> k;

    for (int i = 1; i < (k+1);i++)
    {
        for (int j = 1; j < (k+1); j++)
        {
            for (int l = 1; l < (k+1); l++)
            {
                result += gcd(k,i,j,l);
            }
        }
    }

        cout << result;
}
