import java.util.Scanner;

class Main {

    private static int gcd(int a, int b) {
        while (b != 0) {
            int r = a % b;
            a = b;
            b = r;
        }
        return a;
    }

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

        int res = 0;
        int temp = 0;
        for (int i = 1; i < N + 1; i++) {
            for (int j = 1; j < N + 1; j++) {
                temp = gcd(i, j);
                for (int k = 1; k < N + 1; k++) {
                    res += gcd(k, temp);
                }
            }
        }

        System.out.println(res);
    }
} import java.util.Scanner; Scanner java.util class Main {

    private static int gcd(int a, int b) {
        while (b != 0) {
            int r = a % b;
            a = b;
            b = r;
        }
        return a;
    }

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

        int res = 0;
        int temp = 0;
        for (int i = 1; i < N + 1; i++) {
            for (int j = 1; j < N + 1; j++) {
                temp = gcd(i, j);
                for (int k = 1; k < N + 1; k++) {
                    res += gcd(k, temp);
                }
            }
        }

        System.out.println(res);
    }
} Main private static int gcd(int a, int b) {
        while (b != 0) {
            int r = a % b;
            a = b;
            b = r;
        }
        return a;
    } gcd {
        while (b != 0) {
            int r = a % b;
            a = b;
            b = r;
        }
        return a;
    } while (b != 0) {
            int r = a % b;
            a = b;
            b = r;
        } b != 0 b 0 {
            int r = a % b;
            a = b;
            b = r;
        } int r = a % b; r a % b a b a = b a b b = r b r return a; a int a a int b b public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();

        int res = 0;
        int temp = 0;
        for (int i = 1; i < N + 1; i++) {
            for (int j = 1; j < N + 1; j++) {
                temp = gcd(i, j);
                for (int k = 1; k < N + 1; k++) {
                    res += gcd(k, temp);
                }
            }
        }

        System.out.println(res);
    } main {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();

        int res = 0;
        int temp = 0;
        for (int i = 1; i < N + 1; i++) {
            for (int j = 1; j < N + 1; j++) {
                temp = gcd(i, j);
                for (int k = 1; k < N + 1; k++) {
                    res += gcd(k, temp);
                }
            }
        }

        System.out.println(res);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long N = sc.nextLong(); N sc.nextLong() sc.nextLong sc int res = 0; res 0 int temp = 0; temp 0 for (int i = 1; i < N + 1; i++) {
            for (int j = 1; j < N + 1; j++) {
                temp = gcd(i, j);
                for (int k = 1; k < N + 1; k++) {
                    res += gcd(k, temp);
                }
            }
        } int i = 1; int i = 1; i 1 i < N + 1 i N + 1 N 1 i++ i++ i {
            for (int j = 1; j < N + 1; j++) {
                temp = gcd(i, j);
                for (int k = 1; k < N + 1; k++) {
                    res += gcd(k, temp);
                }
            }
        } {
            for (int j = 1; j < N + 1; j++) {
                temp = gcd(i, j);
                for (int k = 1; k < N + 1; k++) {
                    res += gcd(k, temp);
                }
            }
        } for (int j = 1; j < N + 1; j++) {
                temp = gcd(i, j);
                for (int k = 1; k < N + 1; k++) {
                    res += gcd(k, temp);
                }
            } int j = 1; int j = 1; j 1 j < N + 1 j N + 1 N 1 j++ j++ j {
                temp = gcd(i, j);
                for (int k = 1; k < N + 1; k++) {
                    res += gcd(k, temp);
                }
            } {
                temp = gcd(i, j);
                for (int k = 1; k < N + 1; k++) {
                    res += gcd(k, temp);
                }
            } temp = gcd(i, j) temp gcd(i, j) gcd i j for (int k = 1; k < N + 1; k++) {
                    res += gcd(k, temp);
                } int k = 1; int k = 1; k 1 k < N + 1 k N + 1 N 1 k++ k++ k {
                    res += gcd(k, temp);
                } {
                    res += gcd(k, temp);
                } res += gcd(k, temp) res gcd(k, temp) gcd k temp System.out.println(res) System.out.println System.out System System.out res String[] args args
import java.util.Scanner;

class Main {

    private static int gcd(int a, int b) {
        while (b != 0) {
            int r = a % b;
            a = b;
            b = r;
        }
        return a;
    }

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

        int res = 0;
        int temp = 0;
        for (int i = 1; i < N + 1; i++) {
            for (int j = 1; j < N + 1; j++) {
                temp = gcd(i, j);
                for (int k = 1; k < N + 1; k++) {
                    res += gcd(k, temp);
                }
            }
        }

        System.out.println(res);
    }
}