import java.util.Scanner;
public class Main {
    private static int gcd(int p,int q){
        int temp=p%q;
        while (temp!=0) {
            p = q;
            q = temp;
            temp = p % q;
        }
        return q;
    }

    public static int gcd(int a,int b,int c){
        return gcd(gcd(a,b),c);
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int k=sc.nextInt();

        int sum=0;
        for(int i=1;i<k+1;i++){
            for(int j=1;j<k+1;j++){
                for(int m=1;m<k+1;m++){
                    sum+=gcd(i,j,m);
                }
            }
        }
        System.out.println(sum);


    }
} import java.util.Scanner; Scanner java.util public class Main {
    private static int gcd(int p,int q){
        int temp=p%q;
        while (temp!=0) {
            p = q;
            q = temp;
            temp = p % q;
        }
        return q;
    }

    public static int gcd(int a,int b,int c){
        return gcd(gcd(a,b),c);
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int k=sc.nextInt();

        int sum=0;
        for(int i=1;i<k+1;i++){
            for(int j=1;j<k+1;j++){
                for(int m=1;m<k+1;m++){
                    sum+=gcd(i,j,m);
                }
            }
        }
        System.out.println(sum);


    }
} Main private static int gcd(int p,int q){
        int temp=p%q;
        while (temp!=0) {
            p = q;
            q = temp;
            temp = p % q;
        }
        return q;
    } gcd {
        int temp=p%q;
        while (temp!=0) {
            p = q;
            q = temp;
            temp = p % q;
        }
        return q;
    } int temp=p%q; temp p%q p q while (temp!=0) {
            p = q;
            q = temp;
            temp = p % q;
        } temp!=0 temp 0 {
            p = q;
            q = temp;
            temp = p % q;
        } p = q p q q = temp q temp temp = p % q temp p % q p q return q; q int p p int q q public static int gcd(int a,int b,int c){
        return gcd(gcd(a,b),c);
    } gcd {
        return gcd(gcd(a,b),c);
    } return gcd(gcd(a,b),c); gcd(gcd(a,b),c) gcd gcd(a,b) gcd a b c int a a int b b int c c public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int k=sc.nextInt();

        int sum=0;
        for(int i=1;i<k+1;i++){
            for(int j=1;j<k+1;j++){
                for(int m=1;m<k+1;m++){
                    sum+=gcd(i,j,m);
                }
            }
        }
        System.out.println(sum);


    } main {
        Scanner sc = new Scanner(System.in);
        int k=sc.nextInt();

        int sum=0;
        for(int i=1;i<k+1;i++){
            for(int j=1;j<k+1;j++){
                for(int m=1;m<k+1;m++){
                    sum+=gcd(i,j,m);
                }
            }
        }
        System.out.println(sum);


    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int k=sc.nextInt(); k sc.nextInt() sc.nextInt sc int sum=0; sum 0 for(int i=1;i<k+1;i++){
            for(int j=1;j<k+1;j++){
                for(int m=1;m<k+1;m++){
                    sum+=gcd(i,j,m);
                }
            }
        } int i=1; int i=1; i 1 i<k+1 i k+1 k 1 i++ i++ i {
            for(int j=1;j<k+1;j++){
                for(int m=1;m<k+1;m++){
                    sum+=gcd(i,j,m);
                }
            }
        } {
            for(int j=1;j<k+1;j++){
                for(int m=1;m<k+1;m++){
                    sum+=gcd(i,j,m);
                }
            }
        } for(int j=1;j<k+1;j++){
                for(int m=1;m<k+1;m++){
                    sum+=gcd(i,j,m);
                }
            } int j=1; int j=1; j 1 j<k+1 j k+1 k 1 j++ j++ j {
                for(int m=1;m<k+1;m++){
                    sum+=gcd(i,j,m);
                }
            } {
                for(int m=1;m<k+1;m++){
                    sum+=gcd(i,j,m);
                }
            } for(int m=1;m<k+1;m++){
                    sum+=gcd(i,j,m);
                } int m=1; int m=1; m 1 m<k+1 m k+1 k 1 m++ m++ m {
                    sum+=gcd(i,j,m);
                } {
                    sum+=gcd(i,j,m);
                } sum+=gcd(i,j,m) sum gcd(i,j,m) gcd i j m System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main {
    private static int gcd(int p,int q){
        int temp=p%q;
        while (temp!=0) {
            p = q;
            q = temp;
            temp = p % q;
        }
        return q;
    }

    public static int gcd(int a,int b,int c){
        return gcd(gcd(a,b),c);
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int k=sc.nextInt();

        int sum=0;
        for(int i=1;i<k+1;i++){
            for(int j=1;j<k+1;j++){
                for(int m=1;m<k+1;m++){
                    sum+=gcd(i,j,m);
                }
            }
        }
        System.out.println(sum);


    }
} public class Main {
    private static int gcd(int p,int q){
        int temp=p%q;
        while (temp!=0) {
            p = q;
            q = temp;
            temp = p % q;
        }
        return q;
    }

    public static int gcd(int a,int b,int c){
        return gcd(gcd(a,b),c);
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int k=sc.nextInt();

        int sum=0;
        for(int i=1;i<k+1;i++){
            for(int j=1;j<k+1;j++){
                for(int m=1;m<k+1;m++){
                    sum+=gcd(i,j,m);
                }
            }
        }
        System.out.println(sum);


    }
} Main
import java.util.Scanner;
public class Main {
    private static int gcd(int p,int q){
        int temp=p%q;
        while (temp!=0) {
            p = q;
            q = temp;
            temp = p % q;
        }
        return q;
    }

    public static int gcd(int a,int b,int c){
        return gcd(gcd(a,b),c);
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int k=sc.nextInt();

        int sum=0;
        for(int i=1;i<k+1;i++){
            for(int j=1;j<k+1;j++){
                for(int m=1;m<k+1;m++){
                    sum+=gcd(i,j,m);
                }
            }
        }
        System.out.println(sum);


    }
}
