

import java.util.Scanner;

public class Main {
    static class Solution {
        Integer[][]dp;
        public void find(int k) {
            dp = new Integer[k+1][k+1];
            long res = 0;
            for(int a=1; a<=k; a++) {
                for(int b=1; b<=k; b++) {
                    for(int c=1; c<=k; c++) {
                        int val = gcd(b,c);
                        if(dp[b][c] == null) {
                            dp[b][c] = val;
                        }
                        int val2 = gcd(a, val);
                        if(dp[a][val] == null) {
                            dp[a][val] = val2;
                        }
                        res += val2;
                    }
                }
            }
            System.out.println(res);

        }

        int gcd(int a, int b)
        {
            if(dp[a][b] != null) {
                return dp[a][b];
            }
            // Everything divides 0
            if (a == 0)
                return b;
            if (b == 0)
                return a;

            // base case
            if (a == b)
                return a;

            // a is greater
            if (a > b)
                return gcd(a-b, b);
            return gcd(a, b-a);
        }
    }

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

        int k = sc.nextInt();
        sol.find(k);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    static class Solution {
        Integer[][]dp;
        public void find(int k) {
            dp = new Integer[k+1][k+1];
            long res = 0;
            for(int a=1; a<=k; a++) {
                for(int b=1; b<=k; b++) {
                    for(int c=1; c<=k; c++) {
                        int val = gcd(b,c);
                        if(dp[b][c] == null) {
                            dp[b][c] = val;
                        }
                        int val2 = gcd(a, val);
                        if(dp[a][val] == null) {
                            dp[a][val] = val2;
                        }
                        res += val2;
                    }
                }
            }
            System.out.println(res);

        }

        int gcd(int a, int b)
        {
            if(dp[a][b] != null) {
                return dp[a][b];
            }
            // Everything divides 0
            if (a == 0)
                return b;
            if (b == 0)
                return a;

            // base case
            if (a == b)
                return a;

            // a is greater
            if (a > b)
                return gcd(a-b, b);
            return gcd(a, b-a);
        }
    }

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

        int k = sc.nextInt();
        sol.find(k);
    }
} Main public static void main(String[] args) {
        Solution sol = new Solution();
        Scanner sc = new Scanner(System.in);

        int k = sc.nextInt();
        sol.find(k);
    } main {
        Solution sol = new Solution();
        Scanner sc = new Scanner(System.in);

        int k = sc.nextInt();
        sol.find(k);
    } Solution sol = new Solution(); sol new Solution() Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc sol.find(k) sol.find sol k String[] args args static class Solution {
        Integer[][]dp;
        public void find(int k) {
            dp = new Integer[k+1][k+1];
            long res = 0;
            for(int a=1; a<=k; a++) {
                for(int b=1; b<=k; b++) {
                    for(int c=1; c<=k; c++) {
                        int val = gcd(b,c);
                        if(dp[b][c] == null) {
                            dp[b][c] = val;
                        }
                        int val2 = gcd(a, val);
                        if(dp[a][val] == null) {
                            dp[a][val] = val2;
                        }
                        res += val2;
                    }
                }
            }
            System.out.println(res);

        }

        int gcd(int a, int b)
        {
            if(dp[a][b] != null) {
                return dp[a][b];
            }
            // Everything divides 0
            if (a == 0)
                return b;
            if (b == 0)
                return a;

            // base case
            if (a == b)
                return a;

            // a is greater
            if (a > b)
                return gcd(a-b, b);
            return gcd(a, b-a);
        }
    } Solution Integer[][]dp; dp public void find(int k) {
            dp = new Integer[k+1][k+1];
            long res = 0;
            for(int a=1; a<=k; a++) {
                for(int b=1; b<=k; b++) {
                    for(int c=1; c<=k; c++) {
                        int val = gcd(b,c);
                        if(dp[b][c] == null) {
                            dp[b][c] = val;
                        }
                        int val2 = gcd(a, val);
                        if(dp[a][val] == null) {
                            dp[a][val] = val2;
                        }
                        res += val2;
                    }
                }
            }
            System.out.println(res);

        } find {
            dp = new Integer[k+1][k+1];
            long res = 0;
            for(int a=1; a<=k; a++) {
                for(int b=1; b<=k; b++) {
                    for(int c=1; c<=k; c++) {
                        int val = gcd(b,c);
                        if(dp[b][c] == null) {
                            dp[b][c] = val;
                        }
                        int val2 = gcd(a, val);
                        if(dp[a][val] == null) {
                            dp[a][val] = val2;
                        }
                        res += val2;
                    }
                }
            }
            System.out.println(res);

        } dp = new Integer[k+1][k+1] dp new Integer[k+1][k+1] k+1 k 1 k+1 k 1 long res = 0; res 0 for(int a=1; a<=k; a++) {
                for(int b=1; b<=k; b++) {
                    for(int c=1; c<=k; c++) {
                        int val = gcd(b,c);
                        if(dp[b][c] == null) {
                            dp[b][c] = val;
                        }
                        int val2 = gcd(a, val);
                        if(dp[a][val] == null) {
                            dp[a][val] = val2;
                        }
                        res += val2;
                    }
                }
            } 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++) {
                        int val = gcd(b,c);
                        if(dp[b][c] == null) {
                            dp[b][c] = val;
                        }
                        int val2 = gcd(a, val);
                        if(dp[a][val] == null) {
                            dp[a][val] = val2;
                        }
                        res += val2;
                    }
                }
            } {
                for(int b=1; b<=k; b++) {
                    for(int c=1; c<=k; c++) {
                        int val = gcd(b,c);
                        if(dp[b][c] == null) {
                            dp[b][c] = val;
                        }
                        int val2 = gcd(a, val);
                        if(dp[a][val] == null) {
                            dp[a][val] = val2;
                        }
                        res += val2;
                    }
                }
            } for(int b=1; b<=k; b++) {
                    for(int c=1; c<=k; c++) {
                        int val = gcd(b,c);
                        if(dp[b][c] == null) {
                            dp[b][c] = val;
                        }
                        int val2 = gcd(a, val);
                        if(dp[a][val] == null) {
                            dp[a][val] = val2;
                        }
                        res += val2;
                    }
                } int b=1; int b=1; b 1 b<=k b k b++ b++ b {
                    for(int c=1; c<=k; c++) {
                        int val = gcd(b,c);
                        if(dp[b][c] == null) {
                            dp[b][c] = val;
                        }
                        int val2 = gcd(a, val);
                        if(dp[a][val] == null) {
                            dp[a][val] = val2;
                        }
                        res += val2;
                    }
                } {
                    for(int c=1; c<=k; c++) {
                        int val = gcd(b,c);
                        if(dp[b][c] == null) {
                            dp[b][c] = val;
                        }
                        int val2 = gcd(a, val);
                        if(dp[a][val] == null) {
                            dp[a][val] = val2;
                        }
                        res += val2;
                    }
                } for(int c=1; c<=k; c++) {
                        int val = gcd(b,c);
                        if(dp[b][c] == null) {
                            dp[b][c] = val;
                        }
                        int val2 = gcd(a, val);
                        if(dp[a][val] == null) {
                            dp[a][val] = val2;
                        }
                        res += val2;
                    } int c=1; int c=1; c 1 c<=k c k c++ c++ c {
                        int val = gcd(b,c);
                        if(dp[b][c] == null) {
                            dp[b][c] = val;
                        }
                        int val2 = gcd(a, val);
                        if(dp[a][val] == null) {
                            dp[a][val] = val2;
                        }
                        res += val2;
                    } {
                        int val = gcd(b,c);
                        if(dp[b][c] == null) {
                            dp[b][c] = val;
                        }
                        int val2 = gcd(a, val);
                        if(dp[a][val] == null) {
                            dp[a][val] = val2;
                        }
                        res += val2;
                    } int val = gcd(b,c); val gcd(b,c) gcd b c if(dp[b][c] == null) {
                            dp[b][c] = val;
                        } dp[b][c] == null dp[b][c] dp[b] dp b c null {
                            dp[b][c] = val;
                        } dp[b][c] = val dp[b][c] dp[b] dp b c val int val2 = gcd(a, val); val2 gcd(a, val) gcd a val if(dp[a][val] == null) {
                            dp[a][val] = val2;
                        } dp[a][val] == null dp[a][val] dp[a] dp a val null {
                            dp[a][val] = val2;
                        } dp[a][val] = val2 dp[a][val] dp[a] dp a val val2 res += val2 res val2 System.out.println(res) System.out.println System.out System System.out res int k k int gcd(int a, int b)
        {
            if(dp[a][b] != null) {
                return dp[a][b];
            }
            // Everything divides 0
            if (a == 0)
                return b;
            if (b == 0)
                return a;

            // base case
            if (a == b)
                return a;

            // a is greater
            if (a > b)
                return gcd(a-b, b);
            return gcd(a, b-a);
        } gcd {
            if(dp[a][b] != null) {
                return dp[a][b];
            }
            // Everything divides 0
            if (a == 0)
                return b;
            if (b == 0)
                return a;

            // base case
            if (a == b)
                return a;

            // a is greater
            if (a > b)
                return gcd(a-b, b);
            return gcd(a, b-a);
        } if(dp[a][b] != null) {
                return dp[a][b];
            } dp[a][b] != null dp[a][b] dp[a] dp a b null {
                return dp[a][b];
            } return dp[a][b]; dp[a][b] dp[a] dp a b if (a == 0)
                return b; a == 0 a 0 return b; b if (b == 0)
                return a; b == 0 b 0 return a; a if (a == b)
                return a; a == b a b return a; a if (a > b)
                return gcd(a-b, b); a > b a b return gcd(a-b, b); gcd(a-b, b) gcd a-b a b b return gcd(a, b-a); gcd(a, b-a) gcd a b-a b a int a a int b b public class Main {
    static class Solution {
        Integer[][]dp;
        public void find(int k) {
            dp = new Integer[k+1][k+1];
            long res = 0;
            for(int a=1; a<=k; a++) {
                for(int b=1; b<=k; b++) {
                    for(int c=1; c<=k; c++) {
                        int val = gcd(b,c);
                        if(dp[b][c] == null) {
                            dp[b][c] = val;
                        }
                        int val2 = gcd(a, val);
                        if(dp[a][val] == null) {
                            dp[a][val] = val2;
                        }
                        res += val2;
                    }
                }
            }
            System.out.println(res);

        }

        int gcd(int a, int b)
        {
            if(dp[a][b] != null) {
                return dp[a][b];
            }
            // Everything divides 0
            if (a == 0)
                return b;
            if (b == 0)
                return a;

            // base case
            if (a == b)
                return a;

            // a is greater
            if (a > b)
                return gcd(a-b, b);
            return gcd(a, b-a);
        }
    }

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

        int k = sc.nextInt();
        sol.find(k);
    }
} public class Main {
    static class Solution {
        Integer[][]dp;
        public void find(int k) {
            dp = new Integer[k+1][k+1];
            long res = 0;
            for(int a=1; a<=k; a++) {
                for(int b=1; b<=k; b++) {
                    for(int c=1; c<=k; c++) {
                        int val = gcd(b,c);
                        if(dp[b][c] == null) {
                            dp[b][c] = val;
                        }
                        int val2 = gcd(a, val);
                        if(dp[a][val] == null) {
                            dp[a][val] = val2;
                        }
                        res += val2;
                    }
                }
            }
            System.out.println(res);

        }

        int gcd(int a, int b)
        {
            if(dp[a][b] != null) {
                return dp[a][b];
            }
            // Everything divides 0
            if (a == 0)
                return b;
            if (b == 0)
                return a;

            // base case
            if (a == b)
                return a;

            // a is greater
            if (a > b)
                return gcd(a-b, b);
            return gcd(a, b-a);
        }
    }

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

        int k = sc.nextInt();
        sol.find(k);
    }
} Main


import java.util.Scanner;

public class Main {
    static class Solution {
        Integer[][]dp;
        public void find(int k) {
            dp = new Integer[k+1][k+1];
            long res = 0;
            for(int a=1; a<=k; a++) {
                for(int b=1; b<=k; b++) {
                    for(int c=1; c<=k; c++) {
                        int val = gcd(b,c);
                        if(dp[b][c] == null) {
                            dp[b][c] = val;
                        }
                        int val2 = gcd(a, val);
                        if(dp[a][val] == null) {
                            dp[a][val] = val2;
                        }
                        res += val2;
                    }
                }
            }
            System.out.println(res);

        }

        int gcd(int a, int b)
        {
            if(dp[a][b] != null) {
                return dp[a][b];
            }
            // Everything divides 0
            if (a == 0)
                return b;
            if (b == 0)
                return a;

            // base case
            if (a == b)
                return a;

            // a is greater
            if (a > b)
                return gcd(a-b, b);
            return gcd(a, b-a);
        }
    }

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

        int k = sc.nextInt();
        sol.find(k);
    }
}
