import java.util.*;

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

        int K = sc.nextInt();
        int cnt = 0;

        for (int a = 1; a <= K; a++) {
            for (int b = 1; b <= K; b++) {
                for (int c = 1; c <= K; c++) {
                    cnt += gcd(gcd(a, b), c);
                }
            }
        }

        System.out.println(cnt);
    }

    static int gcd(int m, int n) {
        if (m < n) return gcd(n, m);
        if (n == 0) return m;
        return gcd(n, m % n);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int K = sc.nextInt();
        int cnt = 0;

        for (int a = 1; a <= K; a++) {
            for (int b = 1; b <= K; b++) {
                for (int c = 1; c <= K; c++) {
                    cnt += gcd(gcd(a, b), c);
                }
            }
        }

        System.out.println(cnt);
    }

    static int gcd(int m, int n) {
        if (m < n) return gcd(n, m);
        if (n == 0) return m;
        return gcd(n, m % n);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int K = sc.nextInt();
        int cnt = 0;

        for (int a = 1; a <= K; a++) {
            for (int b = 1; b <= K; b++) {
                for (int c = 1; c <= K; c++) {
                    cnt += gcd(gcd(a, b), c);
                }
            }
        }

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

        int K = sc.nextInt();
        int cnt = 0;

        for (int a = 1; a <= K; a++) {
            for (int b = 1; b <= K; b++) {
                for (int c = 1; c <= K; c++) {
                    cnt += gcd(gcd(a, b), c);
                }
            }
        }

        System.out.println(cnt);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc int cnt = 0; cnt 0 for (int a = 1; a <= K; a++) {
            for (int b = 1; b <= K; b++) {
                for (int c = 1; c <= K; c++) {
                    cnt += gcd(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++) {
                    cnt += gcd(gcd(a, b), c);
                }
            }
        } {
            for (int b = 1; b <= K; b++) {
                for (int c = 1; c <= K; c++) {
                    cnt += gcd(gcd(a, b), c);
                }
            }
        } for (int b = 1; b <= K; b++) {
                for (int c = 1; c <= K; c++) {
                    cnt += gcd(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++) {
                    cnt += gcd(gcd(a, b), c);
                }
            } {
                for (int c = 1; c <= K; c++) {
                    cnt += gcd(gcd(a, b), c);
                }
            } for (int c = 1; c <= K; c++) {
                    cnt += gcd(gcd(a, b), c);
                } int c = 1; int c = 1; c 1 c <= K c K c++ c++ c {
                    cnt += gcd(gcd(a, b), c);
                } {
                    cnt += gcd(gcd(a, b), c);
                } cnt += gcd(gcd(a, b), c) cnt gcd(gcd(a, b), c) gcd gcd(a, b) gcd a b c System.out.println(cnt) System.out.println System.out System System.out cnt String[] args args static int gcd(int m, int n) {
        if (m < n) return gcd(n, m);
        if (n == 0) return m;
        return gcd(n, m % n);
    } gcd {
        if (m < n) return gcd(n, m);
        if (n == 0) return m;
        return gcd(n, m % n);
    } if (m < n) return gcd(n, m); m < n m n return gcd(n, m); gcd(n, m) gcd n m if (n == 0) return m; n == 0 n 0 return m; m return gcd(n, m % n); gcd(n, m % n) gcd n m % n m n int m m int n n public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int K = sc.nextInt();
        int cnt = 0;

        for (int a = 1; a <= K; a++) {
            for (int b = 1; b <= K; b++) {
                for (int c = 1; c <= K; c++) {
                    cnt += gcd(gcd(a, b), c);
                }
            }
        }

        System.out.println(cnt);
    }

    static int gcd(int m, int n) {
        if (m < n) return gcd(n, m);
        if (n == 0) return m;
        return gcd(n, m % n);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int K = sc.nextInt();
        int cnt = 0;

        for (int a = 1; a <= K; a++) {
            for (int b = 1; b <= K; b++) {
                for (int c = 1; c <= K; c++) {
                    cnt += gcd(gcd(a, b), c);
                }
            }
        }

        System.out.println(cnt);
    }

    static int gcd(int m, int n) {
        if (m < n) return gcd(n, m);
        if (n == 0) return m;
        return gcd(n, m % n);
    }
} Main
import java.util.*;

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

        int K = sc.nextInt();
        int cnt = 0;

        for (int a = 1; a <= K; a++) {
            for (int b = 1; b <= K; b++) {
                for (int c = 1; c <= K; c++) {
                    cnt += gcd(gcd(a, b), c);
                }
            }
        }

        System.out.println(cnt);
    }

    static int gcd(int m, int n) {
        if (m < n) return gcd(n, m);
        if (n == 0) return m;
        return gcd(n, m % n);
    }
}
