import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int K = s.nextInt();
        long res = 0;
        for(int a = 1; a <= K; a ++) {
            for (int b = 1; b <= K; b++) {
                for(int c = 1; c <= K; c++) {
                    res += gcd(a, b, c);
                }
            }
        }
        System.out.println(res);
    }
    private static int gcd(int a, int b, int c) {
        return gcd(gcd(a, b), c);
    }
    private static int gcd(int a, int b) {
        if( a == 0) {
            return b;
        }
        return gcd(b%a, a);
    }
} import java.util.Scanner; Scanner java.util class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int K = s.nextInt();
        long res = 0;
        for(int a = 1; a <= K; a ++) {
            for (int b = 1; b <= K; b++) {
                for(int c = 1; c <= K; c++) {
                    res += gcd(a, b, c);
                }
            }
        }
        System.out.println(res);
    }
    private static int gcd(int a, int b, int c) {
        return gcd(gcd(a, b), c);
    }
    private static int gcd(int a, int b) {
        if( a == 0) {
            return b;
        }
        return gcd(b%a, a);
    }
} Main public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int K = s.nextInt();
        long res = 0;
        for(int a = 1; a <= K; a ++) {
            for (int b = 1; b <= K; b++) {
                for(int c = 1; c <= K; c++) {
                    res += gcd(a, b, c);
                }
            }
        }
        System.out.println(res);
    } main {
        Scanner s = new Scanner(System.in);
        int K = s.nextInt();
        long res = 0;
        for(int a = 1; a <= K; a ++) {
            for (int b = 1; b <= K; b++) {
                for(int c = 1; c <= K; c++) {
                    res += gcd(a, b, c);
                }
            }
        }
        System.out.println(res);
    } Scanner s = new Scanner(System.in); s new Scanner(System.in) int K = s.nextInt(); K s.nextInt() s.nextInt s 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++) {
                    res += 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++) {
                    res += gcd(a, b, c);
                }
            }
        } {
            for (int b = 1; b <= K; b++) {
                for(int c = 1; c <= K; c++) {
                    res += gcd(a, b, c);
                }
            }
        } for (int b = 1; b <= K; b++) {
                for(int c = 1; c <= K; c++) {
                    res += 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++) {
                    res += gcd(a, b, c);
                }
            } {
                for(int c = 1; c <= K; c++) {
                    res += gcd(a, b, c);
                }
            } for(int c = 1; c <= K; c++) {
                    res += gcd(a, b, c);
                } int c = 1; int c = 1; c 1 c <= K c K c++ c++ c {
                    res += gcd(a, b, c);
                } {
                    res += gcd(a, b, c);
                } res += gcd(a, b, c) res gcd(a, b, c) gcd a b c System.out.println(res) System.out.println System.out System System.out res String[] args args private static int gcd(int a, int b, int c) {
        return gcd(gcd(a, b), c);
    } gcd {
        return gcd(gcd(a, b), c);
    } return gcd(gcd(a, b), c); gcd(gcd(a, b), c) gcd gcd(a, b) gcd a b c int a a int b b int c c private static int gcd(int a, int b) {
        if( a == 0) {
            return b;
        }
        return gcd(b%a, a);
    } gcd {
        if( a == 0) {
            return b;
        }
        return gcd(b%a, a);
    } if( a == 0) {
            return b;
        } a == 0 a 0 {
            return b;
        } return b; b return gcd(b%a, a); gcd(b%a, a) gcd b%a b a a int a a int b b
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int K = s.nextInt();
        long res = 0;
        for(int a = 1; a <= K; a ++) {
            for (int b = 1; b <= K; b++) {
                for(int c = 1; c <= K; c++) {
                    res += gcd(a, b, c);
                }
            }
        }
        System.out.println(res);
    }
    private static int gcd(int a, int b, int c) {
        return gcd(gcd(a, b), c);
    }
    private static int gcd(int a, int b) {
        if( a == 0) {
            return b;
        }
        return gcd(b%a, a);
    }
}
