import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int K = input.nextInt();
        long ans = 0L;
        for (int i = 1; i < K+1; i++){
            for (int j = 1; j < K+1; j++){
                int tmp = gcd(i,j);
                for (int k = 1; k < K+1; k++){
                    ans += gcd(tmp,k);
                }
            }
        }
        System.out.println(ans);
    }
    static int gcd(int a, int b){
        return b > 0 ? gcd(b,a%b) : a;
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int K = input.nextInt();
        long ans = 0L;
        for (int i = 1; i < K+1; i++){
            for (int j = 1; j < K+1; j++){
                int tmp = gcd(i,j);
                for (int k = 1; k < K+1; k++){
                    ans += gcd(tmp,k);
                }
            }
        }
        System.out.println(ans);
    }
    static int gcd(int a, int b){
        return b > 0 ? gcd(b,a%b) : a;
    }
} Main public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int K = input.nextInt();
        long ans = 0L;
        for (int i = 1; i < K+1; i++){
            for (int j = 1; j < K+1; j++){
                int tmp = gcd(i,j);
                for (int k = 1; k < K+1; k++){
                    ans += gcd(tmp,k);
                }
            }
        }
        System.out.println(ans);
    } main {
        Scanner input = new Scanner(System.in);
        int K = input.nextInt();
        long ans = 0L;
        for (int i = 1; i < K+1; i++){
            for (int j = 1; j < K+1; j++){
                int tmp = gcd(i,j);
                for (int k = 1; k < K+1; k++){
                    ans += gcd(tmp,k);
                }
            }
        }
        System.out.println(ans);
    } Scanner input = new Scanner(System.in); input new Scanner(System.in) int K = input.nextInt(); K input.nextInt() input.nextInt input long ans = 0L; ans 0L for (int i = 1; i < K+1; i++){
            for (int j = 1; j < K+1; j++){
                int tmp = gcd(i,j);
                for (int k = 1; k < K+1; k++){
                    ans += gcd(tmp,k);
                }
            }
        } int i = 1; int i = 1; i 1 i < K+1 i K+1 K 1 i++ i++ i {
            for (int j = 1; j < K+1; j++){
                int tmp = gcd(i,j);
                for (int k = 1; k < K+1; k++){
                    ans += gcd(tmp,k);
                }
            }
        } {
            for (int j = 1; j < K+1; j++){
                int tmp = gcd(i,j);
                for (int k = 1; k < K+1; k++){
                    ans += gcd(tmp,k);
                }
            }
        } for (int j = 1; j < K+1; j++){
                int tmp = gcd(i,j);
                for (int k = 1; k < K+1; k++){
                    ans += gcd(tmp,k);
                }
            } int j = 1; int j = 1; j 1 j < K+1 j K+1 K 1 j++ j++ j {
                int tmp = gcd(i,j);
                for (int k = 1; k < K+1; k++){
                    ans += gcd(tmp,k);
                }
            } {
                int tmp = gcd(i,j);
                for (int k = 1; k < K+1; k++){
                    ans += gcd(tmp,k);
                }
            } int tmp = gcd(i,j); tmp gcd(i,j) gcd i j for (int k = 1; k < K+1; k++){
                    ans += gcd(tmp,k);
                } int k = 1; int k = 1; k 1 k < K+1 k K+1 K 1 k++ k++ k {
                    ans += gcd(tmp,k);
                } {
                    ans += gcd(tmp,k);
                } ans += gcd(tmp,k) ans gcd(tmp,k) gcd tmp k System.out.println(ans) System.out.println System.out System System.out ans String[] args args 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 input = new Scanner(System.in);
        int K = input.nextInt();
        long ans = 0L;
        for (int i = 1; i < K+1; i++){
            for (int j = 1; j < K+1; j++){
                int tmp = gcd(i,j);
                for (int k = 1; k < K+1; k++){
                    ans += gcd(tmp,k);
                }
            }
        }
        System.out.println(ans);
    }
    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 input = new Scanner(System.in);
        int K = input.nextInt();
        long ans = 0L;
        for (int i = 1; i < K+1; i++){
            for (int j = 1; j < K+1; j++){
                int tmp = gcd(i,j);
                for (int k = 1; k < K+1; k++){
                    ans += gcd(tmp,k);
                }
            }
        }
        System.out.println(ans);
    }
    static int gcd(int a, int b){
        return b > 0 ? gcd(b,a%b) : a;
    }
} Main
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int K = input.nextInt();
        long ans = 0L;
        for (int i = 1; i < K+1; i++){
            for (int j = 1; j < K+1; j++){
                int tmp = gcd(i,j);
                for (int k = 1; k < K+1; k++){
                    ans += gcd(tmp,k);
                }
            }
        }
        System.out.println(ans);
    }
    static int gcd(int a, int b){
        return b > 0 ? gcd(b,a%b) : a;
    }
}