import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();

        int ans = 0;
        for (int a = 1; a <= k; a++) {
            for (int b = 1; b <= k; b++) {
                for (int c = 1; c <= k; c++) {
                    ans += gcd(a, b, c);
                }
            }
        }

        System.out.println(ans);
    }

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

    public static int gcd(int x, int y) {
        int ret = 0;
        if (x % y == 0) return y;
        else {
            int z = x % y;
            ret = gcd(y, z);
        }
        return ret;
    }
} 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();

        int ans = 0;
        for (int a = 1; a <= k; a++) {
            for (int b = 1; b <= k; b++) {
                for (int c = 1; c <= k; c++) {
                    ans += gcd(a, b, c);
                }
            }
        }

        System.out.println(ans);
    }

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

    public static int gcd(int x, int y) {
        int ret = 0;
        if (x % y == 0) return y;
        else {
            int z = x % y;
            ret = gcd(y, z);
        }
        return ret;
    }
} Main public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();

        int ans = 0;
        for (int a = 1; a <= k; a++) {
            for (int b = 1; b <= k; b++) {
                for (int c = 1; c <= k; c++) {
                    ans += gcd(a, b, c);
                }
            }
        }

        System.out.println(ans);
    } main {

        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();

        int ans = 0;
        for (int a = 1; a <= k; a++) {
            for (int b = 1; b <= k; b++) {
                for (int c = 1; c <= k; c++) {
                    ans += gcd(a, b, c);
                }
            }
        }

        System.out.println(ans);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int ans = 0; ans 0 for (int a = 1; a <= k; a++) {
            for (int b = 1; b <= k; b++) {
                for (int c = 1; c <= k; c++) {
                    ans += gcd(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++) {
                    ans += gcd(a, b, c);
                }
            }
        } {
            for (int b = 1; b <= k; b++) {
                for (int c = 1; c <= k; c++) {
                    ans += gcd(a, b, c);
                }
            }
        } for (int b = 1; b <= k; b++) {
                for (int c = 1; c <= k; c++) {
                    ans += gcd(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++) {
                    ans += gcd(a, b, c);
                }
            } {
                for (int c = 1; c <= k; c++) {
                    ans += gcd(a, b, c);
                }
            } for (int c = 1; c <= k; c++) {
                    ans += gcd(a, b, c);
                } int c = 1; int c = 1; c 1 c <= k c k c++ c++ c {
                    ans += gcd(a, b, c);
                } {
                    ans += gcd(a, b, c);
                } ans += gcd(a, b, c) ans gcd(a, b, c) gcd a b c System.out.println(ans) System.out.println System.out System System.out ans String[] args args public static int gcd(int a, int b, int c) {
        return gcd(a, gcd(b, c));
    } gcd {
        return gcd(a, gcd(b, c));
    } return gcd(a, gcd(b, c)); gcd(a, gcd(b, c)) gcd a gcd(b, c) gcd b c int a a int b b int c c public static int gcd(int x, int y) {
        int ret = 0;
        if (x % y == 0) return y;
        else {
            int z = x % y;
            ret = gcd(y, z);
        }
        return ret;
    } gcd {
        int ret = 0;
        if (x % y == 0) return y;
        else {
            int z = x % y;
            ret = gcd(y, z);
        }
        return ret;
    } int ret = 0; ret 0 if (x % y == 0) return y;
        else {
            int z = x % y;
            ret = gcd(y, z);
        } x % y == 0 x % y x y 0 return y; y {
            int z = x % y;
            ret = gcd(y, z);
        } int z = x % y; z x % y x y ret = gcd(y, z) ret gcd(y, z) gcd y z return ret; ret int x x int y y public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();

        int ans = 0;
        for (int a = 1; a <= k; a++) {
            for (int b = 1; b <= k; b++) {
                for (int c = 1; c <= k; c++) {
                    ans += gcd(a, b, c);
                }
            }
        }

        System.out.println(ans);
    }

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

    public static int gcd(int x, int y) {
        int ret = 0;
        if (x % y == 0) return y;
        else {
            int z = x % y;
            ret = gcd(y, z);
        }
        return ret;
    }
} public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();

        int ans = 0;
        for (int a = 1; a <= k; a++) {
            for (int b = 1; b <= k; b++) {
                for (int c = 1; c <= k; c++) {
                    ans += gcd(a, b, c);
                }
            }
        }

        System.out.println(ans);
    }

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

    public static int gcd(int x, int y) {
        int ret = 0;
        if (x % y == 0) return y;
        else {
            int z = x % y;
            ret = gcd(y, z);
        }
        return ret;
    }
} Main
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();

        int ans = 0;
        for (int a = 1; a <= k; a++) {
            for (int b = 1; b <= k; b++) {
                for (int c = 1; c <= k; c++) {
                    ans += gcd(a, b, c);
                }
            }
        }

        System.out.println(ans);
    }

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

    public static int gcd(int x, int y) {
        int ret = 0;
        if (x % y == 0) return y;
        else {
            int z = x % y;
            ret = gcd(y, z);
        }
        return ret;
    }
}