import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        int K = sc.nextInt();

        long sum = 0;

        for(int i = 1; i <= K; i++){
            for(int j = 1; j <= K; j++){
                for(int k = 1; k <= K; k++){
                    // System.out.printf("%d %d %d %d\n", i, j, k, gcd(i, j, k));
                    sum += gcd(i, j, k);
                }
            }
        }

        System.out.println(sum);
        sc.close();
    }

    private static int gcd(int a, int b, int c){
        return gcd(a, gcd(b, c));
    }

    private static int gcd(int a, int b){
        if(a >= b){
            if(b == 0){
                return a;
            } else {
                return gcd(b, a%b);
            }
        } else {
            return gcd(b, a);
        }

    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        int K = sc.nextInt();

        long sum = 0;

        for(int i = 1; i <= K; i++){
            for(int j = 1; j <= K; j++){
                for(int k = 1; k <= K; k++){
                    // System.out.printf("%d %d %d %d\n", i, j, k, gcd(i, j, k));
                    sum += gcd(i, j, k);
                }
            }
        }

        System.out.println(sum);
        sc.close();
    }

    private static int gcd(int a, int b, int c){
        return gcd(a, gcd(b, c));
    }

    private static int gcd(int a, int b){
        if(a >= b){
            if(b == 0){
                return a;
            } else {
                return gcd(b, a%b);
            }
        } else {
            return gcd(b, a);
        }

    }
} Main public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        int K = sc.nextInt();

        long sum = 0;

        for(int i = 1; i <= K; i++){
            for(int j = 1; j <= K; j++){
                for(int k = 1; k <= K; k++){
                    // System.out.printf("%d %d %d %d\n", i, j, k, gcd(i, j, k));
                    sum += gcd(i, j, k);
                }
            }
        }

        System.out.println(sum);
        sc.close();
    } main {
        Scanner sc = new Scanner(System.in);

        int K = sc.nextInt();

        long sum = 0;

        for(int i = 1; i <= K; i++){
            for(int j = 1; j <= K; j++){
                for(int k = 1; k <= K; k++){
                    // System.out.printf("%d %d %d %d\n", i, j, k, gcd(i, j, k));
                    sum += gcd(i, j, k);
                }
            }
        }

        System.out.println(sum);
        sc.close();
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc long sum = 0; sum 0 for(int i = 1; i <= K; i++){
            for(int j = 1; j <= K; j++){
                for(int k = 1; k <= K; k++){
                    // System.out.printf("%d %d %d %d\n", i, j, k, gcd(i, j, k));
                    sum += 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++){
                    // System.out.printf("%d %d %d %d\n", i, j, k, gcd(i, j, k));
                    sum += gcd(i, j, k);
                }
            }
        } {
            for(int j = 1; j <= K; j++){
                for(int k = 1; k <= K; k++){
                    // System.out.printf("%d %d %d %d\n", i, j, k, gcd(i, j, k));
                    sum += gcd(i, j, k);
                }
            }
        } for(int j = 1; j <= K; j++){
                for(int k = 1; k <= K; k++){
                    // System.out.printf("%d %d %d %d\n", i, j, k, gcd(i, j, k));
                    sum += 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++){
                    // System.out.printf("%d %d %d %d\n", i, j, k, gcd(i, j, k));
                    sum += gcd(i, j, k);
                }
            } {
                for(int k = 1; k <= K; k++){
                    // System.out.printf("%d %d %d %d\n", i, j, k, gcd(i, j, k));
                    sum += gcd(i, j, k);
                }
            } for(int k = 1; k <= K; k++){
                    // System.out.printf("%d %d %d %d\n", i, j, k, gcd(i, j, k));
                    sum += gcd(i, j, k);
                } int k = 1; int k = 1; k 1 k <= K k K k++ k++ k {
                    // System.out.printf("%d %d %d %d\n", i, j, k, gcd(i, j, k));
                    sum += gcd(i, j, k);
                } {
                    // System.out.printf("%d %d %d %d\n", i, j, k, gcd(i, j, k));
                    sum += gcd(i, j, k);
                } sum += gcd(i, j, k) sum gcd(i, j, k) gcd i j k System.out.println(sum) System.out.println System.out System System.out sum sc.close() sc.close sc String[] args args private static int gcd(int a, int b, int c){
        return gcd(a, gcd(b, c));
    } gcd {
        return gcd(a, gcd(b, c));
    } return gcd(a, gcd(b, c)); gcd(a, gcd(b, c)) gcd a gcd(b, c) gcd b c int a a int b b int c c private static int gcd(int a, int b){
        if(a >= b){
            if(b == 0){
                return a;
            } else {
                return gcd(b, a%b);
            }
        } else {
            return gcd(b, a);
        }

    } gcd {
        if(a >= b){
            if(b == 0){
                return a;
            } else {
                return gcd(b, a%b);
            }
        } else {
            return gcd(b, a);
        }

    } if(a >= b){
            if(b == 0){
                return a;
            } else {
                return gcd(b, a%b);
            }
        } else {
            return gcd(b, a);
        } a >= b a b {
            if(b == 0){
                return a;
            } else {
                return gcd(b, a%b);
            }
        } if(b == 0){
                return a;
            } else {
                return gcd(b, a%b);
            } b == 0 b 0 {
                return a;
            } return a; a {
                return gcd(b, a%b);
            } return gcd(b, a%b); gcd(b, a%b) gcd b a%b a b {
            return gcd(b, a);
        } return gcd(b, a); gcd(b, a) gcd b a int a a int b b public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        int K = sc.nextInt();

        long sum = 0;

        for(int i = 1; i <= K; i++){
            for(int j = 1; j <= K; j++){
                for(int k = 1; k <= K; k++){
                    // System.out.printf("%d %d %d %d\n", i, j, k, gcd(i, j, k));
                    sum += gcd(i, j, k);
                }
            }
        }

        System.out.println(sum);
        sc.close();
    }

    private static int gcd(int a, int b, int c){
        return gcd(a, gcd(b, c));
    }

    private static int gcd(int a, int b){
        if(a >= b){
            if(b == 0){
                return a;
            } else {
                return gcd(b, a%b);
            }
        } else {
            return gcd(b, a);
        }

    }
} public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        int K = sc.nextInt();

        long sum = 0;

        for(int i = 1; i <= K; i++){
            for(int j = 1; j <= K; j++){
                for(int k = 1; k <= K; k++){
                    // System.out.printf("%d %d %d %d\n", i, j, k, gcd(i, j, k));
                    sum += gcd(i, j, k);
                }
            }
        }

        System.out.println(sum);
        sc.close();
    }

    private static int gcd(int a, int b, int c){
        return gcd(a, gcd(b, c));
    }

    private static int gcd(int a, int b){
        if(a >= b){
            if(b == 0){
                return a;
            } else {
                return gcd(b, a%b);
            }
        } else {
            return gcd(b, a);
        }

    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        int K = sc.nextInt();

        long sum = 0;

        for(int i = 1; i <= K; i++){
            for(int j = 1; j <= K; j++){
                for(int k = 1; k <= K; k++){
                    // System.out.printf("%d %d %d %d\n", i, j, k, gcd(i, j, k));
                    sum += gcd(i, j, k);
                }
            }
        }

        System.out.println(sum);
        sc.close();
    }

    private static int gcd(int a, int b, int c){
        return gcd(a, gcd(b, c));
    }

    private static int gcd(int a, int b){
        if(a >= b){
            if(b == 0){
                return a;
            } else {
                return gcd(b, a%b);
            }
        } else {
            return gcd(b, a);
        }

    }
}
