import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static int[][] memo;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int ans = 0;
        memo = new int[k + 1][k + 1];
        for(int[] m : memo) Arrays.fill(m, -1);
        for(int a = 1; a <= k; a++) {
            for(int b = 1; b <= k; b++) {
                for(int c = 1; c <= k; c++) {
                    int d = gcd(a, b);
                    ans += gcd(d, c);
                }
            }
        }
        System.out.println(ans);
    }

    static int gcd(int a, int b) {
        if(memo[a][b] != -1) return memo[a][b];
        int l = Math.max(a, b);
        int m = Math.min(a, b);
        while(m > 0) {
            int t = l % m;
            l = m;
            m = t;
        }
        return memo[a][b] = l;
    }
} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main {
    static int[][] memo;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int ans = 0;
        memo = new int[k + 1][k + 1];
        for(int[] m : memo) Arrays.fill(m, -1);
        for(int a = 1; a <= k; a++) {
            for(int b = 1; b <= k; b++) {
                for(int c = 1; c <= k; c++) {
                    int d = gcd(a, b);
                    ans += gcd(d, c);
                }
            }
        }
        System.out.println(ans);
    }

    static int gcd(int a, int b) {
        if(memo[a][b] != -1) return memo[a][b];
        int l = Math.max(a, b);
        int m = Math.min(a, b);
        while(m > 0) {
            int t = l % m;
            l = m;
            m = t;
        }
        return memo[a][b] = l;
    }
} Main static int[][] memo; memo public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int ans = 0;
        memo = new int[k + 1][k + 1];
        for(int[] m : memo) Arrays.fill(m, -1);
        for(int a = 1; a <= k; a++) {
            for(int b = 1; b <= k; b++) {
                for(int c = 1; c <= k; c++) {
                    int d = gcd(a, b);
                    ans += gcd(d, c);
                }
            }
        }
        System.out.println(ans);
    } main {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int ans = 0;
        memo = new int[k + 1][k + 1];
        for(int[] m : memo) Arrays.fill(m, -1);
        for(int a = 1; a <= k; a++) {
            for(int b = 1; b <= k; b++) {
                for(int c = 1; c <= k; c++) {
                    int d = gcd(a, b);
                    ans += gcd(d, c);
                }
            }
        }
        System.out.println(ans);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int ans = 0; ans 0 memo = new int[k + 1][k + 1] memo new int[k + 1][k + 1] k + 1 k 1 k + 1 k 1 for(int[] m : memo) Arrays.fill(m, -1); int[] m memo Arrays.fill(m, -1); Arrays.fill(m, -1) Arrays.fill Arrays m -1 1 for(int a = 1; a <= k; a++) {
            for(int b = 1; b <= k; b++) {
                for(int c = 1; c <= k; c++) {
                    int d = gcd(a, b);
                    ans += gcd(d, 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++) {
                    int d = gcd(a, b);
                    ans += gcd(d, c);
                }
            }
        } {
            for(int b = 1; b <= k; b++) {
                for(int c = 1; c <= k; c++) {
                    int d = gcd(a, b);
                    ans += gcd(d, c);
                }
            }
        } for(int b = 1; b <= k; b++) {
                for(int c = 1; c <= k; c++) {
                    int d = gcd(a, b);
                    ans += gcd(d, c);
                }
            } int b = 1; int b = 1; b 1 b <= k b k b++ b++ b {
                for(int c = 1; c <= k; c++) {
                    int d = gcd(a, b);
                    ans += gcd(d, c);
                }
            } {
                for(int c = 1; c <= k; c++) {
                    int d = gcd(a, b);
                    ans += gcd(d, c);
                }
            } for(int c = 1; c <= k; c++) {
                    int d = gcd(a, b);
                    ans += gcd(d, c);
                } int c = 1; int c = 1; c 1 c <= k c k c++ c++ c {
                    int d = gcd(a, b);
                    ans += gcd(d, c);
                } {
                    int d = gcd(a, b);
                    ans += gcd(d, c);
                } int d = gcd(a, b); d gcd(a, b) gcd a b ans += gcd(d, c) ans gcd(d, c) gcd d c System.out.println(ans) System.out.println System.out System System.out ans String[] args args static int gcd(int a, int b) {
        if(memo[a][b] != -1) return memo[a][b];
        int l = Math.max(a, b);
        int m = Math.min(a, b);
        while(m > 0) {
            int t = l % m;
            l = m;
            m = t;
        }
        return memo[a][b] = l;
    } gcd {
        if(memo[a][b] != -1) return memo[a][b];
        int l = Math.max(a, b);
        int m = Math.min(a, b);
        while(m > 0) {
            int t = l % m;
            l = m;
            m = t;
        }
        return memo[a][b] = l;
    } if(memo[a][b] != -1) return memo[a][b]; memo[a][b] != -1 memo[a][b] memo[a] memo a b -1 1 return memo[a][b]; memo[a][b] memo[a] memo a b int l = Math.max(a, b); l Math.max(a, b) Math.max Math a b int m = Math.min(a, b); m Math.min(a, b) Math.min Math a b while(m > 0) {
            int t = l % m;
            l = m;
            m = t;
        } m > 0 m 0 {
            int t = l % m;
            l = m;
            m = t;
        } int t = l % m; t l % m l m l = m l m m = t m t return memo[a][b] = l; memo[a][b] = l memo[a][b] memo[a] memo a b l int a a int b b public class Main {
    static int[][] memo;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int ans = 0;
        memo = new int[k + 1][k + 1];
        for(int[] m : memo) Arrays.fill(m, -1);
        for(int a = 1; a <= k; a++) {
            for(int b = 1; b <= k; b++) {
                for(int c = 1; c <= k; c++) {
                    int d = gcd(a, b);
                    ans += gcd(d, c);
                }
            }
        }
        System.out.println(ans);
    }

    static int gcd(int a, int b) {
        if(memo[a][b] != -1) return memo[a][b];
        int l = Math.max(a, b);
        int m = Math.min(a, b);
        while(m > 0) {
            int t = l % m;
            l = m;
            m = t;
        }
        return memo[a][b] = l;
    }
} public class Main {
    static int[][] memo;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int ans = 0;
        memo = new int[k + 1][k + 1];
        for(int[] m : memo) Arrays.fill(m, -1);
        for(int a = 1; a <= k; a++) {
            for(int b = 1; b <= k; b++) {
                for(int c = 1; c <= k; c++) {
                    int d = gcd(a, b);
                    ans += gcd(d, c);
                }
            }
        }
        System.out.println(ans);
    }

    static int gcd(int a, int b) {
        if(memo[a][b] != -1) return memo[a][b];
        int l = Math.max(a, b);
        int m = Math.min(a, b);
        while(m > 0) {
            int t = l % m;
            l = m;
            m = t;
        }
        return memo[a][b] = l;
    }
} Main
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static int[][] memo;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int ans = 0;
        memo = new int[k + 1][k + 1];
        for(int[] m : memo) Arrays.fill(m, -1);
        for(int a = 1; a <= k; a++) {
            for(int b = 1; b <= k; b++) {
                for(int c = 1; c <= k; c++) {
                    int d = gcd(a, b);
                    ans += gcd(d, c);
                }
            }
        }
        System.out.println(ans);
    }

    static int gcd(int a, int b) {
        if(memo[a][b] != -1) return memo[a][b];
        int l = Math.max(a, b);
        int m = Math.min(a, b);
        while(m > 0) {
            int t = l % m;
            l = m;
            m = t;
        }
        return memo[a][b] = l;
    }
}
