import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int K = in.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++){
                    int d = gcd(i,j);
                    d = gcd(k,d);
                    sum+=d;
                }
            }
        }
        System.out.print(sum);
    }

    public static int gcd(int a, int b){
        return (b>0)?gcd(b, a%b):a;
    }

} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int K = in.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++){
                    int d = gcd(i,j);
                    d = gcd(k,d);
                    sum+=d;
                }
            }
        }
        System.out.print(sum);
    }

    public static int gcd(int a, int b){
        return (b>0)?gcd(b, a%b):a;
    }

} Main public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int K = in.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++){
                    int d = gcd(i,j);
                    d = gcd(k,d);
                    sum+=d;
                }
            }
        }
        System.out.print(sum);
    } main {
        Scanner in = new Scanner(System.in);
        int K = in.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++){
                    int d = gcd(i,j);
                    d = gcd(k,d);
                    sum+=d;
                }
            }
        }
        System.out.print(sum);
    } Scanner in = new Scanner(System.in); in new Scanner(System.in) int K = in.nextInt(); K in.nextInt() in.nextInt in 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++){
                    int d = gcd(i,j);
                    d = gcd(k,d);
                    sum+=d;
                }
            }
        } 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++){
                    int d = gcd(i,j);
                    d = gcd(k,d);
                    sum+=d;
                }
            }
        } {
            for(int j = 1; j<=K; j++){
                for(int k = 1; k<=K; k++){
                    int d = gcd(i,j);
                    d = gcd(k,d);
                    sum+=d;
                }
            }
        } for(int j = 1; j<=K; j++){
                for(int k = 1; k<=K; k++){
                    int d = gcd(i,j);
                    d = gcd(k,d);
                    sum+=d;
                }
            } int j = 1; int j = 1; j 1 j<=K j K j++ j++ j {
                for(int k = 1; k<=K; k++){
                    int d = gcd(i,j);
                    d = gcd(k,d);
                    sum+=d;
                }
            } {
                for(int k = 1; k<=K; k++){
                    int d = gcd(i,j);
                    d = gcd(k,d);
                    sum+=d;
                }
            } for(int k = 1; k<=K; k++){
                    int d = gcd(i,j);
                    d = gcd(k,d);
                    sum+=d;
                } int k = 1; int k = 1; k 1 k<=K k K k++ k++ k {
                    int d = gcd(i,j);
                    d = gcd(k,d);
                    sum+=d;
                } {
                    int d = gcd(i,j);
                    d = gcd(k,d);
                    sum+=d;
                } int d = gcd(i,j); d gcd(i,j) gcd i j d = gcd(k,d) d gcd(k,d) gcd k d sum+=d sum d System.out.print(sum) System.out.print System.out System System.out sum String[] args args public static int gcd(int a, int b){
        return (b>0)?gcd(b, a%b):a;
    } gcd {
        return (b>0)?gcd(b, a%b):a;
    } return (b>0)?gcd(b, a%b):a; (b>0)?gcd(b, a%b):a (b>0) b 0 gcd(b, a%b) gcd b a%b a b a int a a int b b public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int K = in.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++){
                    int d = gcd(i,j);
                    d = gcd(k,d);
                    sum+=d;
                }
            }
        }
        System.out.print(sum);
    }

    public static int gcd(int a, int b){
        return (b>0)?gcd(b, a%b):a;
    }

} public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int K = in.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++){
                    int d = gcd(i,j);
                    d = gcd(k,d);
                    sum+=d;
                }
            }
        }
        System.out.print(sum);
    }

    public static int gcd(int a, int b){
        return (b>0)?gcd(b, a%b):a;
    }

} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int K = in.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++){
                    int d = gcd(i,j);
                    d = gcd(k,d);
                    sum+=d;
                }
            }
        }
        System.out.print(sum);
    }

    public static int gcd(int a, int b){
        return (b>0)?gcd(b, a%b):a;
    }

}
