import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int K = s.nextInt();
        int gcd = 0;

        for (int i = 1; i <= K; i++) {
            for (int j = 1; j <= K; j++) {
                for (int k = 1; k <= K; k++) {
                    gcd += gcd(gcd(j, k), i);
                }
            }
        }

        System.out.println(gcd);
        s.close();
    }

    public static int gcd(int p, int q) {
        if (p % q == 0) {
            return q;
        }
        return gcd(q, p % q);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int K = s.nextInt();
        int gcd = 0;

        for (int i = 1; i <= K; i++) {
            for (int j = 1; j <= K; j++) {
                for (int k = 1; k <= K; k++) {
                    gcd += gcd(gcd(j, k), i);
                }
            }
        }

        System.out.println(gcd);
        s.close();
    }

    public static int gcd(int p, int q) {
        if (p % q == 0) {
            return q;
        }
        return gcd(q, p % q);
    }
} Main public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int K = s.nextInt();
        int gcd = 0;

        for (int i = 1; i <= K; i++) {
            for (int j = 1; j <= K; j++) {
                for (int k = 1; k <= K; k++) {
                    gcd += gcd(gcd(j, k), i);
                }
            }
        }

        System.out.println(gcd);
        s.close();
    } main {
        Scanner s = new Scanner(System.in);
        int K = s.nextInt();
        int gcd = 0;

        for (int i = 1; i <= K; i++) {
            for (int j = 1; j <= K; j++) {
                for (int k = 1; k <= K; k++) {
                    gcd += gcd(gcd(j, k), i);
                }
            }
        }

        System.out.println(gcd);
        s.close();
    } Scanner s = new Scanner(System.in); s new Scanner(System.in) int K = s.nextInt(); K s.nextInt() s.nextInt s int gcd = 0; gcd 0 for (int i = 1; i <= K; i++) {
            for (int j = 1; j <= K; j++) {
                for (int k = 1; k <= K; k++) {
                    gcd += gcd(gcd(j, k), i);
                }
            }
        } int i = 1; int i = 1; i 1 i <= K i K i++ i++ i {
            for (int j = 1; j <= K; j++) {
                for (int k = 1; k <= K; k++) {
                    gcd += gcd(gcd(j, k), i);
                }
            }
        } {
            for (int j = 1; j <= K; j++) {
                for (int k = 1; k <= K; k++) {
                    gcd += gcd(gcd(j, k), i);
                }
            }
        } for (int j = 1; j <= K; j++) {
                for (int k = 1; k <= K; k++) {
                    gcd += gcd(gcd(j, k), i);
                }
            } int j = 1; int j = 1; j 1 j <= K j K j++ j++ j {
                for (int k = 1; k <= K; k++) {
                    gcd += gcd(gcd(j, k), i);
                }
            } {
                for (int k = 1; k <= K; k++) {
                    gcd += gcd(gcd(j, k), i);
                }
            } for (int k = 1; k <= K; k++) {
                    gcd += gcd(gcd(j, k), i);
                } int k = 1; int k = 1; k 1 k <= K k K k++ k++ k {
                    gcd += gcd(gcd(j, k), i);
                } {
                    gcd += gcd(gcd(j, k), i);
                } gcd += gcd(gcd(j, k), i) gcd gcd(gcd(j, k), i) gcd gcd(j, k) gcd j k i System.out.println(gcd) System.out.println System.out System System.out gcd s.close() s.close s String[] args args public static int gcd(int p, int q) {
        if (p % q == 0) {
            return q;
        }
        return gcd(q, p % q);
    } gcd {
        if (p % q == 0) {
            return q;
        }
        return gcd(q, p % q);
    } if (p % q == 0) {
            return q;
        } p % q == 0 p % q p q 0 {
            return q;
        } return q; q return gcd(q, p % q); gcd(q, p % q) gcd q p % q p q int p p int q q public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int K = s.nextInt();
        int gcd = 0;

        for (int i = 1; i <= K; i++) {
            for (int j = 1; j <= K; j++) {
                for (int k = 1; k <= K; k++) {
                    gcd += gcd(gcd(j, k), i);
                }
            }
        }

        System.out.println(gcd);
        s.close();
    }

    public static int gcd(int p, int q) {
        if (p % q == 0) {
            return q;
        }
        return gcd(q, p % q);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int K = s.nextInt();
        int gcd = 0;

        for (int i = 1; i <= K; i++) {
            for (int j = 1; j <= K; j++) {
                for (int k = 1; k <= K; k++) {
                    gcd += gcd(gcd(j, k), i);
                }
            }
        }

        System.out.println(gcd);
        s.close();
    }

    public static int gcd(int p, int q) {
        if (p % q == 0) {
            return q;
        }
        return gcd(q, p % q);
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int K = s.nextInt();
        int gcd = 0;

        for (int i = 1; i <= K; i++) {
            for (int j = 1; j <= K; j++) {
                for (int k = 1; k <= K; k++) {
                    gcd += gcd(gcd(j, k), i);
                }
            }
        }

        System.out.println(gcd);
        s.close();
    }

    public static int gcd(int p, int q) {
        if (p % q == 0) {
            return q;
        }
        return gcd(q, p % q);
    }
}