import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);

        final int x = sc.nextInt();

        long ans = 0;
        for (int i = 1; i <= x; i++) {
            for (int j = 1; j <= x; j++) {
                for (int k = 1; k <= x; k++) {
                    ans += gcd(gcd(i, j), k);
                }
            }
        }

        System.out.println(ans);
    }

    private static long gcd(long a, long b) {
        if (b == 0) return a;
        return gcd(b, a % b);
    }
} import java.util.Scanner; Scanner java.util public class Main {

    public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);

        final int x = sc.nextInt();

        long ans = 0;
        for (int i = 1; i <= x; i++) {
            for (int j = 1; j <= x; j++) {
                for (int k = 1; k <= x; k++) {
                    ans += gcd(gcd(i, j), k);
                }
            }
        }

        System.out.println(ans);
    }

    private static long gcd(long a, long b) {
        if (b == 0) return a;
        return gcd(b, a % b);
    }
} Main public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);

        final int x = sc.nextInt();

        long ans = 0;
        for (int i = 1; i <= x; i++) {
            for (int j = 1; j <= x; j++) {
                for (int k = 1; k <= x; k++) {
                    ans += gcd(gcd(i, j), k);
                }
            }
        }

        System.out.println(ans);
    } main {
        final Scanner sc = new Scanner(System.in);

        final int x = sc.nextInt();

        long ans = 0;
        for (int i = 1; i <= x; i++) {
            for (int j = 1; j <= x; j++) {
                for (int k = 1; k <= x; k++) {
                    ans += gcd(gcd(i, j), k);
                }
            }
        }

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

    public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);

        final int x = sc.nextInt();

        long ans = 0;
        for (int i = 1; i <= x; i++) {
            for (int j = 1; j <= x; j++) {
                for (int k = 1; k <= x; k++) {
                    ans += gcd(gcd(i, j), k);
                }
            }
        }

        System.out.println(ans);
    }

    private static long gcd(long a, long b) {
        if (b == 0) return a;
        return gcd(b, a % b);
    }
} public class Main {

    public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);

        final int x = sc.nextInt();

        long ans = 0;
        for (int i = 1; i <= x; i++) {
            for (int j = 1; j <= x; j++) {
                for (int k = 1; k <= x; k++) {
                    ans += gcd(gcd(i, j), k);
                }
            }
        }

        System.out.println(ans);
    }

    private static long gcd(long a, long b) {
        if (b == 0) return a;
        return gcd(b, a % b);
    }
} Main
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);

        final int x = sc.nextInt();

        long ans = 0;
        for (int i = 1; i <= x; i++) {
            for (int j = 1; j <= x; j++) {
                for (int k = 1; k <= x; k++) {
                    ans += gcd(gcd(i, j), k);
                }
            }
        }

        System.out.println(ans);
    }

    private static long gcd(long a, long b) {
        if (b == 0) return a;
        return gcd(b, a % b);
    }
}
