import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Integer K = input.nextInt();
        long sum = 0;

        for (int a = 1; a <= K; a++) {
            for (int b = 1; b <= K; b++) {
                for (int c = 1; c <= K; c++) {
                    sum += gcd_triple(a, b, c);
                }
            }
        }
        System.out.println(sum);
    }

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

    private static int gcd(int n, int m) {
        int bigger = Integer.max(n,m);
        int smaller = Integer.min(n,m);
        int sur = bigger % smaller;

        if (sur == 0) {
            return smaller;
        } else {
            return gcd(smaller, sur);
        }

    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Integer K = input.nextInt();
        long sum = 0;

        for (int a = 1; a <= K; a++) {
            for (int b = 1; b <= K; b++) {
                for (int c = 1; c <= K; c++) {
                    sum += gcd_triple(a, b, c);
                }
            }
        }
        System.out.println(sum);
    }

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

    private static int gcd(int n, int m) {
        int bigger = Integer.max(n,m);
        int smaller = Integer.min(n,m);
        int sur = bigger % smaller;

        if (sur == 0) {
            return smaller;
        } else {
            return gcd(smaller, sur);
        }

    }
} Main public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Integer K = input.nextInt();
        long sum = 0;

        for (int a = 1; a <= K; a++) {
            for (int b = 1; b <= K; b++) {
                for (int c = 1; c <= K; c++) {
                    sum += gcd_triple(a, b, c);
                }
            }
        }
        System.out.println(sum);
    } main {
        Scanner input = new Scanner(System.in);
        Integer K = input.nextInt();
        long sum = 0;

        for (int a = 1; a <= K; a++) {
            for (int b = 1; b <= K; b++) {
                for (int c = 1; c <= K; c++) {
                    sum += gcd_triple(a, b, c);
                }
            }
        }
        System.out.println(sum);
    } Scanner input = new Scanner(System.in); input new Scanner(System.in) Integer K = input.nextInt(); K input.nextInt() input.nextInt input long sum = 0; sum 0 for (int a = 1; a <= K; a++) {
            for (int b = 1; b <= K; b++) {
                for (int c = 1; c <= K; c++) {
                    sum += gcd_triple(a, b, c);
                }
            }
        } int a = 1; int a = 1; a 1 a <= K a K a++ a++ a {
            for (int b = 1; b <= K; b++) {
                for (int c = 1; c <= K; c++) {
                    sum += gcd_triple(a, b, c);
                }
            }
        } {
            for (int b = 1; b <= K; b++) {
                for (int c = 1; c <= K; c++) {
                    sum += gcd_triple(a, b, c);
                }
            }
        } for (int b = 1; b <= K; b++) {
                for (int c = 1; c <= K; c++) {
                    sum += gcd_triple(a, b, c);
                }
            } int b = 1; int b = 1; b 1 b <= K b K b++ b++ b {
                for (int c = 1; c <= K; c++) {
                    sum += gcd_triple(a, b, c);
                }
            } {
                for (int c = 1; c <= K; c++) {
                    sum += gcd_triple(a, b, c);
                }
            } for (int c = 1; c <= K; c++) {
                    sum += gcd_triple(a, b, c);
                } int c = 1; int c = 1; c 1 c <= K c K c++ c++ c {
                    sum += gcd_triple(a, b, c);
                } {
                    sum += gcd_triple(a, b, c);
                } sum += gcd_triple(a, b, c) sum gcd_triple(a, b, c) gcd_triple a b c System.out.println(sum) System.out.println System.out System System.out sum String[] args args private static int gcd_triple(int a, int b, int c) {
        return gcd(gcd(a, b), c);
    } gcd_triple {
        return gcd(gcd(a, b), c);
    } return gcd(gcd(a, b), c); gcd(gcd(a, b), c) gcd gcd(a, b) gcd a b c int a a int b b int c c private static int gcd(int n, int m) {
        int bigger = Integer.max(n,m);
        int smaller = Integer.min(n,m);
        int sur = bigger % smaller;

        if (sur == 0) {
            return smaller;
        } else {
            return gcd(smaller, sur);
        }

    } gcd {
        int bigger = Integer.max(n,m);
        int smaller = Integer.min(n,m);
        int sur = bigger % smaller;

        if (sur == 0) {
            return smaller;
        } else {
            return gcd(smaller, sur);
        }

    } int bigger = Integer.max(n,m); bigger Integer.max(n,m) Integer.max Integer n m int smaller = Integer.min(n,m); smaller Integer.min(n,m) Integer.min Integer n m int sur = bigger % smaller; sur bigger % smaller bigger smaller if (sur == 0) {
            return smaller;
        } else {
            return gcd(smaller, sur);
        } sur == 0 sur 0 {
            return smaller;
        } return smaller; smaller {
            return gcd(smaller, sur);
        } return gcd(smaller, sur); gcd(smaller, sur) gcd smaller sur int n n int m m public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Integer K = input.nextInt();
        long sum = 0;

        for (int a = 1; a <= K; a++) {
            for (int b = 1; b <= K; b++) {
                for (int c = 1; c <= K; c++) {
                    sum += gcd_triple(a, b, c);
                }
            }
        }
        System.out.println(sum);
    }

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

    private static int gcd(int n, int m) {
        int bigger = Integer.max(n,m);
        int smaller = Integer.min(n,m);
        int sur = bigger % smaller;

        if (sur == 0) {
            return smaller;
        } else {
            return gcd(smaller, sur);
        }

    }
} public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Integer K = input.nextInt();
        long sum = 0;

        for (int a = 1; a <= K; a++) {
            for (int b = 1; b <= K; b++) {
                for (int c = 1; c <= K; c++) {
                    sum += gcd_triple(a, b, c);
                }
            }
        }
        System.out.println(sum);
    }

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

    private static int gcd(int n, int m) {
        int bigger = Integer.max(n,m);
        int smaller = Integer.min(n,m);
        int sur = bigger % smaller;

        if (sur == 0) {
            return smaller;
        } else {
            return gcd(smaller, sur);
        }

    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Integer K = input.nextInt();
        long sum = 0;

        for (int a = 1; a <= K; a++) {
            for (int b = 1; b <= K; b++) {
                for (int c = 1; c <= K; c++) {
                    sum += gcd_triple(a, b, c);
                }
            }
        }
        System.out.println(sum);
    }

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

    private static int gcd(int n, int m) {
        int bigger = Integer.max(n,m);
        int smaller = Integer.min(n,m);
        int sur = bigger % smaller;

        if (sur == 0) {
            return smaller;
        } else {
            return gcd(smaller, sur);
        }

    }
}