import java.util.Scanner;
import java.lang.Math;
class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        sc.close();

        int M = 0;
        for(int i = 1;i<=N;i++){
            for(int j = 1;j<=N;j++){
                // M = M + gcd(i,j);
                for(int k = 1;k<=N;k++){
                    M = M + gcd(i,gcd(j,k));
                }
            }
        }
        System.out.print(M);
    }

    public static int gcd(int a,int b){
        int c = a;
        a = Math.min(a,b);
        b = Math.max(c,b);

        while(b%a != 0){
            c = a;
            a = b%a;
            b = c;
        }
        return a;
    }
} import java.util.Scanner; Scanner java.util import java.lang.Math; Math java.lang class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        sc.close();

        int M = 0;
        for(int i = 1;i<=N;i++){
            for(int j = 1;j<=N;j++){
                // M = M + gcd(i,j);
                for(int k = 1;k<=N;k++){
                    M = M + gcd(i,gcd(j,k));
                }
            }
        }
        System.out.print(M);
    }

    public static int gcd(int a,int b){
        int c = a;
        a = Math.min(a,b);
        b = Math.max(c,b);

        while(b%a != 0){
            c = a;
            a = b%a;
            b = c;
        }
        return a;
    }
} Main public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        sc.close();

        int M = 0;
        for(int i = 1;i<=N;i++){
            for(int j = 1;j<=N;j++){
                // M = M + gcd(i,j);
                for(int k = 1;k<=N;k++){
                    M = M + gcd(i,gcd(j,k));
                }
            }
        }
        System.out.print(M);
    } main {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        sc.close();

        int M = 0;
        for(int i = 1;i<=N;i++){
            for(int j = 1;j<=N;j++){
                // M = M + gcd(i,j);
                for(int k = 1;k<=N;k++){
                    M = M + gcd(i,gcd(j,k));
                }
            }
        }
        System.out.print(M);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc sc.close() sc.close sc int M = 0; M 0 for(int i = 1;i<=N;i++){
            for(int j = 1;j<=N;j++){
                // M = M + gcd(i,j);
                for(int k = 1;k<=N;k++){
                    M = M + gcd(i,gcd(j,k));
                }
            }
        } int i = 1; int i = 1; i 1 i<=N i N i++ i++ i {
            for(int j = 1;j<=N;j++){
                // M = M + gcd(i,j);
                for(int k = 1;k<=N;k++){
                    M = M + gcd(i,gcd(j,k));
                }
            }
        } {
            for(int j = 1;j<=N;j++){
                // M = M + gcd(i,j);
                for(int k = 1;k<=N;k++){
                    M = M + gcd(i,gcd(j,k));
                }
            }
        } for(int j = 1;j<=N;j++){
                // M = M + gcd(i,j);
                for(int k = 1;k<=N;k++){
                    M = M + gcd(i,gcd(j,k));
                }
            } int j = 1; int j = 1; j 1 j<=N j N j++ j++ j {
                // M = M + gcd(i,j);
                for(int k = 1;k<=N;k++){
                    M = M + gcd(i,gcd(j,k));
                }
            } {
                // M = M + gcd(i,j);
                for(int k = 1;k<=N;k++){
                    M = M + gcd(i,gcd(j,k));
                }
            } for(int k = 1;k<=N;k++){
                    M = M + gcd(i,gcd(j,k));
                } int k = 1; int k = 1; k 1 k<=N k N k++ k++ k {
                    M = M + gcd(i,gcd(j,k));
                } {
                    M = M + gcd(i,gcd(j,k));
                } M = M + gcd(i,gcd(j,k)) M M + gcd(i,gcd(j,k)) M gcd(i,gcd(j,k)) gcd i gcd(j,k) gcd j k System.out.print(M) System.out.print System.out System System.out M String[] args args public static int gcd(int a,int b){
        int c = a;
        a = Math.min(a,b);
        b = Math.max(c,b);

        while(b%a != 0){
            c = a;
            a = b%a;
            b = c;
        }
        return a;
    } gcd {
        int c = a;
        a = Math.min(a,b);
        b = Math.max(c,b);

        while(b%a != 0){
            c = a;
            a = b%a;
            b = c;
        }
        return a;
    } int c = a; c a a = Math.min(a,b) a Math.min(a,b) Math.min Math a b b = Math.max(c,b) b Math.max(c,b) Math.max Math c b while(b%a != 0){
            c = a;
            a = b%a;
            b = c;
        } b%a != 0 b%a b a 0 {
            c = a;
            a = b%a;
            b = c;
        } c = a c a a = b%a a b%a b a b = c b c return a; a int a a int b b
import java.util.Scanner;
import java.lang.Math;
class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        sc.close();

        int M = 0;
        for(int i = 1;i<=N;i++){
            for(int j = 1;j<=N;j++){
                // M = M + gcd(i,j);
                for(int k = 1;k<=N;k++){
                    M = M + gcd(i,gcd(j,k));
                }
            }
        }
        System.out.print(M);
    }

    public static int gcd(int a,int b){
        int c = a;
        a = Math.min(a,b);
        b = Math.max(c,b);

        while(b%a != 0){
            c = a;
            a = b%a;
            b = c;
        }
        return a;
    }
}