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

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

    public static int gcd3(int x, int y, int z){
        int xy = gcd(x,y);
        return gcd(xy,z);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long sum = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                for(int k = 1; k <= n; k++){
                    sum += gcd3(i,j,k);
                }
            }
        }
        System.out.println(sum);
    }

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

    public static int gcd3(int x, int y, int z){
        int xy = gcd(x,y);
        return gcd(xy,z);
    }
} Main public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long sum = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                for(int k = 1; k <= n; k++){
                    sum += gcd3(i,j,k);
                }
            }
        }
        System.out.println(sum);
    } main {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long sum = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                for(int k = 1; k <= n; k++){
                    sum += gcd3(i,j,k);
                }
            }
        }
        System.out.println(sum);
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int n = scanner.nextInt(); n scanner.nextInt() scanner.nextInt scanner long sum = 0; sum 0 for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                for(int k = 1; k <= n; k++){
                    sum += gcd3(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++){
                    sum += gcd3(i,j,k);
                }
            }
        } {
            for(int j = 1; j <= n; j++){
                for(int k = 1; k <= n; k++){
                    sum += gcd3(i,j,k);
                }
            }
        } for(int j = 1; j <= n; j++){
                for(int k = 1; k <= n; k++){
                    sum += gcd3(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++){
                    sum += gcd3(i,j,k);
                }
            } {
                for(int k = 1; k <= n; k++){
                    sum += gcd3(i,j,k);
                }
            } for(int k = 1; k <= n; k++){
                    sum += gcd3(i,j,k);
                } int k = 1; int k = 1; k 1 k <= n k n k++ k++ k {
                    sum += gcd3(i,j,k);
                } {
                    sum += gcd3(i,j,k);
                } sum += gcd3(i,j,k) sum gcd3(i,j,k) gcd3 i j k System.out.println(sum) System.out.println System.out System System.out sum String[] args args public static int gcd(int a, int b){
        if(b == 0){return a;}
        return gcd(b, a%b);
    } gcd {
        if(b == 0){return a;}
        return gcd(b, a%b);
    } if(b == 0){return a;} b == 0 b 0 {return a;} return a; a return gcd(b, a%b); gcd(b, a%b) gcd b a%b a b int a a int b b public static int gcd3(int x, int y, int z){
        int xy = gcd(x,y);
        return gcd(xy,z);
    } gcd3 {
        int xy = gcd(x,y);
        return gcd(xy,z);
    } int xy = gcd(x,y); xy gcd(x,y) gcd x y return gcd(xy,z); gcd(xy,z) gcd xy z int x x int y y int z z public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long sum = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                for(int k = 1; k <= n; k++){
                    sum += gcd3(i,j,k);
                }
            }
        }
        System.out.println(sum);
    }

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

    public static int gcd3(int x, int y, int z){
        int xy = gcd(x,y);
        return gcd(xy,z);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long sum = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                for(int k = 1; k <= n; k++){
                    sum += gcd3(i,j,k);
                }
            }
        }
        System.out.println(sum);
    }

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

    public static int gcd3(int x, int y, int z){
        int xy = gcd(x,y);
        return gcd(xy,z);
    }
} Main
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long sum = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                for(int k = 1; k <= n; k++){
                    sum += gcd3(i,j,k);
                }
            }
        }
        System.out.println(sum);
    }

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

    public static int gcd3(int x, int y, int z){
        int xy = gcd(x,y);
        return gcd(xy,z);
    }
}