import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        abc162_c solver = new abc162_c();
        solver.solve(1, in, out);
        out.close();
    }

    static class abc162_c {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int K = in.nextInt();
            long 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(gcd(a, b), c);
                    }
                }

            }
            out.println(ans);
        }

        private long gcd(long m, long n) {
            if (m < n) return gcd(n, m);
            if (n == 0) return m;
            return gcd(n, m % n);
        }

    }
}
 import java.io.OutputStream; OutputStream java.io import java.io.IOException; IOException java.io import java.io.InputStream; InputStream java.io import java.io.PrintWriter; PrintWriter java.io import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        abc162_c solver = new abc162_c();
        solver.solve(1, in, out);
        out.close();
    }

    static class abc162_c {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int K = in.nextInt();
            long 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(gcd(a, b), c);
                    }
                }

            }
            out.println(ans);
        }

        private long gcd(long m, long 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) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        abc162_c solver = new abc162_c();
        solver.solve(1, in, out);
        out.close();
    } main {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        abc162_c solver = new abc162_c();
        solver.solve(1, in, out);
        out.close();
    } InputStream inputStream = System.in; inputStream System.in System System.in OutputStream outputStream = System.out; outputStream System.out System System.out Scanner in = new Scanner(inputStream); in new Scanner(inputStream) PrintWriter out = new PrintWriter(outputStream); out new PrintWriter(outputStream) abc162_c solver = new abc162_c(); solver new abc162_c() solver.solve(1, in, out) solver.solve solver 1 in out out.close() out.close out String[] args args static class abc162_c {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int K = in.nextInt();
            long 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(gcd(a, b), c);
                    }
                }

            }
            out.println(ans);
        }

        private long gcd(long m, long n) {
            if (m < n) return gcd(n, m);
            if (n == 0) return m;
            return gcd(n, m % n);
        }

    } abc162_c public void solve(int testNumber, Scanner in, PrintWriter out) {
            int K = in.nextInt();
            long 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(gcd(a, b), c);
                    }
                }

            }
            out.println(ans);
        } solve {
            int K = in.nextInt();
            long 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(gcd(a, b), c);
                    }
                }

            }
            out.println(ans);
        } int K = in.nextInt(); K in.nextInt() in.nextInt in long 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(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(gcd(a, b), c);
                    }
                }

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

            } for (int b = 1; b <= K; b++) {
                    for (int c = 1; c <= K; c++) {
                        ans += 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++) {
                        ans += gcd(gcd(a, b), c);
                    }
                } {
                    for (int c = 1; c <= K; c++) {
                        ans += gcd(gcd(a, b), c);
                    }
                } for (int c = 1; c <= K; c++) {
                        ans += gcd(gcd(a, b), c);
                    } int c = 1; int c = 1; c 1 c <= K c K c++ c++ c {
                        ans += gcd(gcd(a, b), c);
                    } {
                        ans += gcd(gcd(a, b), c);
                    } ans += gcd(gcd(a, b), c) ans gcd(gcd(a, b), c) gcd gcd(a, b) gcd a b c out.println(ans) out.println out ans int testNumber testNumber Scanner in in PrintWriter out out private long gcd(long m, long 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 long m m long n n public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        abc162_c solver = new abc162_c();
        solver.solve(1, in, out);
        out.close();
    }

    static class abc162_c {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int K = in.nextInt();
            long 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(gcd(a, b), c);
                    }
                }

            }
            out.println(ans);
        }

        private long gcd(long m, long 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) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        abc162_c solver = new abc162_c();
        solver.solve(1, in, out);
        out.close();
    }

    static class abc162_c {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int K = in.nextInt();
            long 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(gcd(a, b), c);
                    }
                }

            }
            out.println(ans);
        }

        private long gcd(long m, long n) {
            if (m < n) return gcd(n, m);
            if (n == 0) return m;
            return gcd(n, m % n);
        }

    }
} Main
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        abc162_c solver = new abc162_c();
        solver.solve(1, in, out);
        out.close();
    }

    static class abc162_c {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int K = in.nextInt();
            long 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(gcd(a, b), c);
                    }
                }

            }
            out.println(ans);
        }

        private long gcd(long m, long n) {
            if (m < n) return gcd(n, m);
            if (n == 0) return m;
            return gcd(n, m % n);
        }

    }
}

