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);
        SumofgcdofTuples solver = new SumofgcdofTuples();
        solver.solve(1, in, out);
        out.close();
    }

    static class SumofgcdofTuples {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int k = in.nextInt();

            long result = 0;
            for (int i = 1; i <= k; i++) {
                for (int j = 1; j <= k; j++) {
                    for (int l = 1; l <= k; l++) {
                        result += gcd(gcd(i, j), l);
                    }
                }
            }
            out.append(String.valueOf(result));
        }

        static long gcd(long a, long b) {
            return b == 0 ? a : gcd(b, a % b);
        }

    }
}
 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);
        SumofgcdofTuples solver = new SumofgcdofTuples();
        solver.solve(1, in, out);
        out.close();
    }

    static class SumofgcdofTuples {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int k = in.nextInt();

            long result = 0;
            for (int i = 1; i <= k; i++) {
                for (int j = 1; j <= k; j++) {
                    for (int l = 1; l <= k; l++) {
                        result += gcd(gcd(i, j), l);
                    }
                }
            }
            out.append(String.valueOf(result));
        }

        static long gcd(long a, long b) {
            return b == 0 ? a : gcd(b, a % b);
        }

    }
} 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);
        SumofgcdofTuples solver = new SumofgcdofTuples();
        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);
        SumofgcdofTuples solver = new SumofgcdofTuples();
        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) SumofgcdofTuples solver = new SumofgcdofTuples(); solver new SumofgcdofTuples() solver.solve(1, in, out) solver.solve solver 1 in out out.close() out.close out String[] args args static class SumofgcdofTuples {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int k = in.nextInt();

            long result = 0;
            for (int i = 1; i <= k; i++) {
                for (int j = 1; j <= k; j++) {
                    for (int l = 1; l <= k; l++) {
                        result += gcd(gcd(i, j), l);
                    }
                }
            }
            out.append(String.valueOf(result));
        }

        static long gcd(long a, long b) {
            return b == 0 ? a : gcd(b, a % b);
        }

    } SumofgcdofTuples public void solve(int testNumber, Scanner in, PrintWriter out) {
            int k = in.nextInt();

            long result = 0;
            for (int i = 1; i <= k; i++) {
                for (int j = 1; j <= k; j++) {
                    for (int l = 1; l <= k; l++) {
                        result += gcd(gcd(i, j), l);
                    }
                }
            }
            out.append(String.valueOf(result));
        } solve {
            int k = in.nextInt();

            long result = 0;
            for (int i = 1; i <= k; i++) {
                for (int j = 1; j <= k; j++) {
                    for (int l = 1; l <= k; l++) {
                        result += gcd(gcd(i, j), l);
                    }
                }
            }
            out.append(String.valueOf(result));
        } int k = in.nextInt(); k in.nextInt() in.nextInt in long result = 0; result 0 for (int i = 1; i <= k; i++) {
                for (int j = 1; j <= k; j++) {
                    for (int l = 1; l <= k; l++) {
                        result += gcd(gcd(i, j), l);
                    }
                }
            } int i = 1; int i = 1; i 1 i <= k i k i++ i++ i {
                for (int j = 1; j <= k; j++) {
                    for (int l = 1; l <= k; l++) {
                        result += gcd(gcd(i, j), l);
                    }
                }
            } {
                for (int j = 1; j <= k; j++) {
                    for (int l = 1; l <= k; l++) {
                        result += gcd(gcd(i, j), l);
                    }
                }
            } for (int j = 1; j <= k; j++) {
                    for (int l = 1; l <= k; l++) {
                        result += gcd(gcd(i, j), l);
                    }
                } int j = 1; int j = 1; j 1 j <= k j k j++ j++ j {
                    for (int l = 1; l <= k; l++) {
                        result += gcd(gcd(i, j), l);
                    }
                } {
                    for (int l = 1; l <= k; l++) {
                        result += gcd(gcd(i, j), l);
                    }
                } for (int l = 1; l <= k; l++) {
                        result += gcd(gcd(i, j), l);
                    } int l = 1; int l = 1; l 1 l <= k l k l++ l++ l {
                        result += gcd(gcd(i, j), l);
                    } {
                        result += gcd(gcd(i, j), l);
                    } result += gcd(gcd(i, j), l) result gcd(gcd(i, j), l) gcd gcd(i, j) gcd i j l out.append(String.valueOf(result)) out.append out String.valueOf(result) String.valueOf String result int testNumber testNumber Scanner in in PrintWriter out out static long gcd(long a, long b) {
            return b == 0 ? a : gcd(b, a % b);
        } gcd {
            return b == 0 ? a : gcd(b, a % b);
        } return b == 0 ? a : gcd(b, a % b); b == 0 ? a : gcd(b, a % b) b == 0 b 0 a 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) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        SumofgcdofTuples solver = new SumofgcdofTuples();
        solver.solve(1, in, out);
        out.close();
    }

    static class SumofgcdofTuples {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int k = in.nextInt();

            long result = 0;
            for (int i = 1; i <= k; i++) {
                for (int j = 1; j <= k; j++) {
                    for (int l = 1; l <= k; l++) {
                        result += gcd(gcd(i, j), l);
                    }
                }
            }
            out.append(String.valueOf(result));
        }

        static long gcd(long a, long b) {
            return b == 0 ? a : gcd(b, a % b);
        }

    }
} 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);
        SumofgcdofTuples solver = new SumofgcdofTuples();
        solver.solve(1, in, out);
        out.close();
    }

    static class SumofgcdofTuples {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int k = in.nextInt();

            long result = 0;
            for (int i = 1; i <= k; i++) {
                for (int j = 1; j <= k; j++) {
                    for (int l = 1; l <= k; l++) {
                        result += gcd(gcd(i, j), l);
                    }
                }
            }
            out.append(String.valueOf(result));
        }

        static long gcd(long a, long b) {
            return b == 0 ? a : gcd(b, a % b);
        }

    }
} 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);
        SumofgcdofTuples solver = new SumofgcdofTuples();
        solver.solve(1, in, out);
        out.close();
    }

    static class SumofgcdofTuples {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int k = in.nextInt();

            long result = 0;
            for (int i = 1; i <= k; i++) {
                for (int j = 1; j <= k; j++) {
                    for (int l = 1; l <= k; l++) {
                        result += gcd(gcd(i, j), l);
                    }
                }
            }
            out.append(String.valueOf(result));
        }

        static long gcd(long a, long b) {
            return b == 0 ? a : gcd(b, a % b);
        }

    }
}

