using System;
using System.Collections.Generic;
using System.Linq;

namespace AtCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            //[summary]C - Sum of gcd of Tuples (Easy)
            int K = int.Parse(Console.ReadLine());
            var nums = new List<int[]>();

            for(int i = 1; i <= K; i++)
            {
                for (int j = 1; j <= K; j++)
                {
                    for (int k = 1; k <= K; k++)
                    {
                        var arr = new int[3];
                        arr[0] = i;
                        arr[1] = j;
                        arr[2] = k;
                        nums.Add(arr);
                    }
                }
            }

            long ans = 0;

            foreach(var n in nums)
            {
                ans += GetGcd(n);
            }

            Console.WriteLine(ans);
        }

        static long GetGcd(int[] nums)
        {
            var a = (long)nums[0];
            var b = (long)nums[1];
            var c = (long)nums[2];

            var lib = new Library.Gcd();
            var gcd = lib.GetGcd(a, b);
            return lib.GetGcd(gcd, c);
        }
    }
}

namespace Library
{
    public class Gcd
    {
        public long GetGcd(long a, long b)
        {
            //[summary]a,b2つの数の最大公約数を求める
            //Gcd: Greatest Common Divisor
            //以下リンクを参考に実装
            //https://qiita.com/gushwell/items/e9614b4ac2bea3fc6486

            if (a < b)
            {
                //引数を入れ替えて自分を呼び出す
                return GetGcd(b, a);
            }
            else
            {
                while (b > 0)
                {
                    long remainder = a % b;

                    a = b;
                    b = remainder;
                }

                return a;
            }
        }
    }
}