import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int K = scanner.nextInt();

        int num = 0;
        for (int i = 1; i < K + 1; i++) {
            for (int j = 1; j < K + 1; j++) {
                int cache = gcd(i, j);
                for (int k = 1; k < K + 1; k++) {
                    num += gcd(cache, k);
                }
            }
        }
        System.out.println(num);
    }

    public static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        } else {
            a = a % b; // 残り部分
            return gcd(b, a); // 残り部分から最小の正方形を見つける
        }
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int K = scanner.nextInt();

        int num = 0;
        for (int i = 1; i < K + 1; i++) {
            for (int j = 1; j < K + 1; j++) {
                int cache = gcd(i, j);
                for (int k = 1; k < K + 1; k++) {
                    num += gcd(cache, k);
                }
            }
        }
        System.out.println(num);
    }

    public static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        } else {
            a = a % b; // 残り部分
            return gcd(b, a); // 残り部分から最小の正方形を見つける
        }
    }
} Main public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int K = scanner.nextInt();

        int num = 0;
        for (int i = 1; i < K + 1; i++) {
            for (int j = 1; j < K + 1; j++) {
                int cache = gcd(i, j);
                for (int k = 1; k < K + 1; k++) {
                    num += gcd(cache, k);
                }
            }
        }
        System.out.println(num);
    } main {
        Scanner scanner = new Scanner(System.in);
        int K = scanner.nextInt();

        int num = 0;
        for (int i = 1; i < K + 1; i++) {
            for (int j = 1; j < K + 1; j++) {
                int cache = gcd(i, j);
                for (int k = 1; k < K + 1; k++) {
                    num += gcd(cache, k);
                }
            }
        }
        System.out.println(num);
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int K = scanner.nextInt(); K scanner.nextInt() scanner.nextInt scanner int num = 0; num 0 for (int i = 1; i < K + 1; i++) {
            for (int j = 1; j < K + 1; j++) {
                int cache = gcd(i, j);
                for (int k = 1; k < K + 1; k++) {
                    num += gcd(cache, k);
                }
            }
        } int i = 1; int i = 1; i 1 i < K + 1 i K + 1 K 1 i++ i++ i {
            for (int j = 1; j < K + 1; j++) {
                int cache = gcd(i, j);
                for (int k = 1; k < K + 1; k++) {
                    num += gcd(cache, k);
                }
            }
        } {
            for (int j = 1; j < K + 1; j++) {
                int cache = gcd(i, j);
                for (int k = 1; k < K + 1; k++) {
                    num += gcd(cache, k);
                }
            }
        } for (int j = 1; j < K + 1; j++) {
                int cache = gcd(i, j);
                for (int k = 1; k < K + 1; k++) {
                    num += gcd(cache, k);
                }
            } int j = 1; int j = 1; j 1 j < K + 1 j K + 1 K 1 j++ j++ j {
                int cache = gcd(i, j);
                for (int k = 1; k < K + 1; k++) {
                    num += gcd(cache, k);
                }
            } {
                int cache = gcd(i, j);
                for (int k = 1; k < K + 1; k++) {
                    num += gcd(cache, k);
                }
            } int cache = gcd(i, j); cache gcd(i, j) gcd i j for (int k = 1; k < K + 1; k++) {
                    num += gcd(cache, k);
                } int k = 1; int k = 1; k 1 k < K + 1 k K + 1 K 1 k++ k++ k {
                    num += gcd(cache, k);
                } {
                    num += gcd(cache, k);
                } num += gcd(cache, k) num gcd(cache, k) gcd cache k System.out.println(num) System.out.println System.out System System.out num String[] args args public static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        } else {
            a = a % b; // 残り部分
            return gcd(b, a); // 残り部分から最小の正方形を見つける
        }
    } gcd {
        if (b == 0) {
            return a;
        } else {
            a = a % b; // 残り部分
            return gcd(b, a); // 残り部分から最小の正方形を見つける
        }
    } if (b == 0) {
            return a;
        } else {
            a = a % b; // 残り部分
            return gcd(b, a); // 残り部分から最小の正方形を見つける
        } b == 0 b 0 {
            return a;
        } return a; a {
            a = a % b; // 残り部分
            return gcd(b, a); // 残り部分から最小の正方形を見つける
        } a = a % b a a % b a b return gcd(b, a); // 残り部分から最小の正方形を見つける gcd(b, a) gcd b a int a a int b b public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int K = scanner.nextInt();

        int num = 0;
        for (int i = 1; i < K + 1; i++) {
            for (int j = 1; j < K + 1; j++) {
                int cache = gcd(i, j);
                for (int k = 1; k < K + 1; k++) {
                    num += gcd(cache, k);
                }
            }
        }
        System.out.println(num);
    }

    public static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        } else {
            a = a % b; // 残り部分
            return gcd(b, a); // 残り部分から最小の正方形を見つける
        }
    }
} public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int K = scanner.nextInt();

        int num = 0;
        for (int i = 1; i < K + 1; i++) {
            for (int j = 1; j < K + 1; j++) {
                int cache = gcd(i, j);
                for (int k = 1; k < K + 1; k++) {
                    num += gcd(cache, k);
                }
            }
        }
        System.out.println(num);
    }

    public static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        } else {
            a = a % b; // 残り部分
            return gcd(b, a); // 残り部分から最小の正方形を見つける
        }
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int K = scanner.nextInt();

        int num = 0;
        for (int i = 1; i < K + 1; i++) {
            for (int j = 1; j < K + 1; j++) {
                int cache = gcd(i, j);
                for (int k = 1; k < K + 1; k++) {
                    num += gcd(cache, k);
                }
            }
        }
        System.out.println(num);
    }

    public static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        } else {
            a = a % b; // 残り部分
            return gcd(b, a); // 残り部分から最小の正方形を見つける
        }
    }
}