import java.util.Scanner;

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 m = 1; m<=K; m++) {
                    sum+=gcd(gcd(i, j), m);
                }
            }
        }
        
        System.out.println(sum);
        sc.close();
    }
    private static long gcd(long m, long n) {
        if(m < n) return gcd(n, m);
        if(n == 0) return m;
        return gcd(n, m % n);
    }
} import java.util.Scanner; Scanner 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 m = 1; m<=K; m++) {
                    sum+=gcd(gcd(i, j), m);
                }
            }
        }
        
        System.out.println(sum);
        sc.close();
    }
    private static long gcd(long m, long n) {
        if(m < n) return gcd(n, m);
        if(n == 0) return m;
        return gcd(n, m % n);
    }
} 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 m = 1; m<=K; m++) {
                    sum+=gcd(gcd(i, j), m);
                }
            }
        }
        
        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 m = 1; m<=K; m++) {
                    sum+=gcd(gcd(i, j), m);
                }
            }
        }
        
        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 m = 1; m<=K; m++) {
                    sum+=gcd(gcd(i, j), m);
                }
            }
        } int i = 1; int i = 1; i 1 i <= K i K i++ i++ i {
            for (int j = 1; j <=K; j++) {
                for (int m = 1; m<=K; m++) {
                    sum+=gcd(gcd(i, j), m);
                }
            }
        } {
            for (int j = 1; j <=K; j++) {
                for (int m = 1; m<=K; m++) {
                    sum+=gcd(gcd(i, j), m);
                }
            }
        } for (int j = 1; j <=K; j++) {
                for (int m = 1; m<=K; m++) {
                    sum+=gcd(gcd(i, j), m);
                }
            } int j = 1; int j = 1; j 1 j <=K j K j++ j++ j {
                for (int m = 1; m<=K; m++) {
                    sum+=gcd(gcd(i, j), m);
                }
            } {
                for (int m = 1; m<=K; m++) {
                    sum+=gcd(gcd(i, j), m);
                }
            } for (int m = 1; m<=K; m++) {
                    sum+=gcd(gcd(i, j), m);
                } int m = 1; int m = 1; m 1 m<=K m K m++ m++ m {
                    sum+=gcd(gcd(i, j), m);
                } {
                    sum+=gcd(gcd(i, j), m);
                } sum+=gcd(gcd(i, j), m) sum gcd(gcd(i, j), m) gcd gcd(i, j) gcd i j m System.out.println(sum) System.out.println System.out System System.out sum sc.close() sc.close sc String[] args args private static long gcd(long m, long n) {
        if(m < n) return gcd(n, m);
        if(n == 0) return m;
        return gcd(n, m % n);
    } gcd {
        if(m < n) return gcd(n, m);
        if(n == 0) return m;
        return gcd(n, m % n);
    } if(m < n) return gcd(n, m); m < n m n return gcd(n, m); gcd(n, m) gcd n m if(n == 0) return m; n == 0 n 0 return m; m return gcd(n, m % n); gcd(n, m % n) gcd n m % n m n long m m long n n 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 m = 1; m<=K; m++) {
                    sum+=gcd(gcd(i, j), m);
                }
            }
        }
        
        System.out.println(sum);
        sc.close();
    }
    private static long gcd(long m, long n) {
        if(m < n) return gcd(n, m);
        if(n == 0) return m;
        return gcd(n, m % n);
    }
} 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 m = 1; m<=K; m++) {
                    sum+=gcd(gcd(i, j), m);
                }
            }
        }
        
        System.out.println(sum);
        sc.close();
    }
    private static long gcd(long m, long n) {
        if(m < n) return gcd(n, m);
        if(n == 0) return m;
        return gcd(n, m % n);
    }
} Main
import java.util.Scanner;

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 m = 1; m<=K; m++) {
                    sum+=gcd(gcd(i, j), m);
                }
            }
        }
        
        System.out.println(sum);
        sc.close();
    }
    private static long gcd(long m, long n) {
        if(m < n) return gcd(n, m);
        if(n == 0) return m;
        return gcd(n, m % n);
    }
}