import java.util.*;
public class Main {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long total=0;
        for(int i=1; i<=N; i++){
            for(int j=1; j<=N; j++){
                for(int k=1; k<=N; k++){
                    total += gcd(gcd(i,j),k);
                }
            }
        }
        System.out.println(total);
    }

    static long gcd(long x, long y){
        if(0 == y){
            return x;
        }
        return gcd(y, x%y); 
    }
} import java.util.*; util.* java public class Main {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long total=0;
        for(int i=1; i<=N; i++){
            for(int j=1; j<=N; j++){
                for(int k=1; k<=N; k++){
                    total += gcd(gcd(i,j),k);
                }
            }
        }
        System.out.println(total);
    }

    static long gcd(long x, long y){
        if(0 == y){
            return x;
        }
        return gcd(y, x%y); 
    }
} Main public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long total=0;
        for(int i=1; i<=N; i++){
            for(int j=1; j<=N; j++){
                for(int k=1; k<=N; k++){
                    total += gcd(gcd(i,j),k);
                }
            }
        }
        System.out.println(total);
    } main {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long total=0;
        for(int i=1; i<=N; i++){
            for(int j=1; j<=N; j++){
                for(int k=1; k<=N; k++){
                    total += gcd(gcd(i,j),k);
                }
            }
        }
        System.out.println(total);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc long total=0; total 0 for(int i=1; i<=N; i++){
            for(int j=1; j<=N; j++){
                for(int k=1; k<=N; k++){
                    total += gcd(gcd(i,j),k);
                }
            }
        } int i=1; int i=1; i 1 i<=N i N i++ i++ i {
            for(int j=1; j<=N; j++){
                for(int k=1; k<=N; k++){
                    total += gcd(gcd(i,j),k);
                }
            }
        } {
            for(int j=1; j<=N; j++){
                for(int k=1; k<=N; k++){
                    total += gcd(gcd(i,j),k);
                }
            }
        } for(int j=1; j<=N; j++){
                for(int k=1; k<=N; k++){
                    total += gcd(gcd(i,j),k);
                }
            } int j=1; int j=1; j 1 j<=N j N j++ j++ j {
                for(int k=1; k<=N; k++){
                    total += gcd(gcd(i,j),k);
                }
            } {
                for(int k=1; k<=N; k++){
                    total += gcd(gcd(i,j),k);
                }
            } for(int k=1; k<=N; k++){
                    total += gcd(gcd(i,j),k);
                } int k=1; int k=1; k 1 k<=N k N k++ k++ k {
                    total += gcd(gcd(i,j),k);
                } {
                    total += gcd(gcd(i,j),k);
                } total += gcd(gcd(i,j),k) total gcd(gcd(i,j),k) gcd gcd(i,j) gcd i j k System.out.println(total) System.out.println System.out System System.out total String args[] args static long gcd(long x, long y){
        if(0 == y){
            return x;
        }
        return gcd(y, x%y); 
    } gcd {
        if(0 == y){
            return x;
        }
        return gcd(y, x%y); 
    } if(0 == y){
            return x;
        } 0 == y 0 y {
            return x;
        } return x; x return gcd(y, x%y); gcd(y, x%y) gcd y x%y x y long x x long y y public class Main {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long total=0;
        for(int i=1; i<=N; i++){
            for(int j=1; j<=N; j++){
                for(int k=1; k<=N; k++){
                    total += gcd(gcd(i,j),k);
                }
            }
        }
        System.out.println(total);
    }

    static long gcd(long x, long y){
        if(0 == y){
            return x;
        }
        return gcd(y, x%y); 
    }
} public class Main {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long total=0;
        for(int i=1; i<=N; i++){
            for(int j=1; j<=N; j++){
                for(int k=1; k<=N; k++){
                    total += gcd(gcd(i,j),k);
                }
            }
        }
        System.out.println(total);
    }

    static long gcd(long x, long y){
        if(0 == y){
            return x;
        }
        return gcd(y, x%y); 
    }
} Main
import java.util.*;
public class Main {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long total=0;
        for(int i=1; i<=N; i++){
            for(int j=1; j<=N; j++){
                for(int k=1; k<=N; k++){
                    total += gcd(gcd(i,j),k);
                }
            }
        }
        System.out.println(total);
    }

    static long gcd(long x, long y){
        if(0 == y){
            return x;
        }
        return gcd(y, x%y); 
    }
}