import java.util.Scanner;

public class Main {
    public static int gcd(int a, int b)
    {
        while (a*b != 0){
            if (a > b){
                a %= b;
            }else{
                b %= a;
            }
        }
        return a + b;
    }
    public static void main(String[] args) {
        int K;
        Long sum = 0L;
        Scanner sc = new Scanner(System.in);
        K = sc.nextInt();
        for(int i = 1 ; i <= K ; i++)
        {
            for(int j = 1 ; j <= K ; j++)
            {
                for(int k = 1 ; k <= K ; k++)
                {
                    sum += gcd(gcd(i,j),k);
                }
            }
        }
        System.out.println(sum);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static int gcd(int a, int b)
    {
        while (a*b != 0){
            if (a > b){
                a %= b;
            }else{
                b %= a;
            }
        }
        return a + b;
    }
    public static void main(String[] args) {
        int K;
        Long sum = 0L;
        Scanner sc = new Scanner(System.in);
        K = sc.nextInt();
        for(int i = 1 ; i <= K ; i++)
        {
            for(int j = 1 ; j <= K ; j++)
            {
                for(int k = 1 ; k <= K ; k++)
                {
                    sum += gcd(gcd(i,j),k);
                }
            }
        }
        System.out.println(sum);
    }
} Main public static int gcd(int a, int b)
    {
        while (a*b != 0){
            if (a > b){
                a %= b;
            }else{
                b %= a;
            }
        }
        return a + b;
    } gcd {
        while (a*b != 0){
            if (a > b){
                a %= b;
            }else{
                b %= a;
            }
        }
        return a + b;
    } while (a*b != 0){
            if (a > b){
                a %= b;
            }else{
                b %= a;
            }
        } a*b != 0 a*b a b 0 {
            if (a > b){
                a %= b;
            }else{
                b %= a;
            }
        } if (a > b){
                a %= b;
            }else{
                b %= a;
            } a > b a b {
                a %= b;
            } a %= b a b {
                b %= a;
            } b %= a b a return a + b; a + b a b int a a int b b public static void main(String[] args) {
        int K;
        Long sum = 0L;
        Scanner sc = new Scanner(System.in);
        K = sc.nextInt();
        for(int i = 1 ; i <= K ; i++)
        {
            for(int j = 1 ; j <= K ; j++)
            {
                for(int k = 1 ; k <= K ; k++)
                {
                    sum += gcd(gcd(i,j),k);
                }
            }
        }
        System.out.println(sum);
    } main {
        int K;
        Long sum = 0L;
        Scanner sc = new Scanner(System.in);
        K = sc.nextInt();
        for(int i = 1 ; i <= K ; i++)
        {
            for(int j = 1 ; j <= K ; j++)
            {
                for(int k = 1 ; k <= K ; k++)
                {
                    sum += gcd(gcd(i,j),k);
                }
            }
        }
        System.out.println(sum);
    } int K; K Long sum = 0L; sum 0L Scanner sc = new Scanner(System.in); sc new Scanner(System.in) K = sc.nextInt() K sc.nextInt() sc.nextInt sc for(int i = 1 ; i <= K ; i++)
        {
            for(int j = 1 ; j <= K ; j++)
            {
                for(int k = 1 ; k <= K ; k++)
                {
                    sum += gcd(gcd(i,j),k);
                }
            }
        } int i = 1 ; int i = 1 ; i 1 i <= K i K i++ i++ i {
            for(int j = 1 ; j <= K ; j++)
            {
                for(int k = 1 ; k <= K ; k++)
                {
                    sum += gcd(gcd(i,j),k);
                }
            }
        } {
            for(int j = 1 ; j <= K ; j++)
            {
                for(int k = 1 ; k <= K ; k++)
                {
                    sum += gcd(gcd(i,j),k);
                }
            }
        } for(int j = 1 ; j <= K ; j++)
            {
                for(int k = 1 ; k <= K ; k++)
                {
                    sum += gcd(gcd(i,j),k);
                }
            } int j = 1 ; int j = 1 ; j 1 j <= K j K j++ j++ j {
                for(int k = 1 ; k <= K ; k++)
                {
                    sum += gcd(gcd(i,j),k);
                }
            } {
                for(int k = 1 ; k <= K ; k++)
                {
                    sum += gcd(gcd(i,j),k);
                }
            } for(int k = 1 ; k <= K ; k++)
                {
                    sum += gcd(gcd(i,j),k);
                } int k = 1 ; int k = 1 ; k 1 k <= K k K k++ k++ k {
                    sum += gcd(gcd(i,j),k);
                } {
                    sum += gcd(gcd(i,j),k);
                } sum += gcd(gcd(i,j),k) sum gcd(gcd(i,j),k) gcd gcd(i,j) gcd i j k System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main {
    public static int gcd(int a, int b)
    {
        while (a*b != 0){
            if (a > b){
                a %= b;
            }else{
                b %= a;
            }
        }
        return a + b;
    }
    public static void main(String[] args) {
        int K;
        Long sum = 0L;
        Scanner sc = new Scanner(System.in);
        K = sc.nextInt();
        for(int i = 1 ; i <= K ; i++)
        {
            for(int j = 1 ; j <= K ; j++)
            {
                for(int k = 1 ; k <= K ; k++)
                {
                    sum += gcd(gcd(i,j),k);
                }
            }
        }
        System.out.println(sum);
    }
} public class Main {
    public static int gcd(int a, int b)
    {
        while (a*b != 0){
            if (a > b){
                a %= b;
            }else{
                b %= a;
            }
        }
        return a + b;
    }
    public static void main(String[] args) {
        int K;
        Long sum = 0L;
        Scanner sc = new Scanner(System.in);
        K = sc.nextInt();
        for(int i = 1 ; i <= K ; i++)
        {
            for(int j = 1 ; j <= K ; j++)
            {
                for(int k = 1 ; k <= K ; k++)
                {
                    sum += gcd(gcd(i,j),k);
                }
            }
        }
        System.out.println(sum);
    }
} Main
import java.util.Scanner;

public class Main {
    public static int gcd(int a, int b)
    {
        while (a*b != 0){
            if (a > b){
                a %= b;
            }else{
                b %= a;
            }
        }
        return a + b;
    }
    public static void main(String[] args) {
        int K;
        Long sum = 0L;
        Scanner sc = new Scanner(System.in);
        K = sc.nextInt();
        for(int i = 1 ; i <= K ; i++)
        {
            for(int j = 1 ; j <= K ; j++)
            {
                for(int k = 1 ; k <= K ; k++)
                {
                    sum += gcd(gcd(i,j),k);
                }
            }
        }
        System.out.println(sum);
    }
}
