import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int ans = 0;

        for (int i = 1; i < N+1; i++) {
            for (int j = 1; j < N+1; j++) {
                for (int k = 1; k <  N+1; k++) {
                        ans += gcd(gcd(i, j), k);
                }
            }
        }

        System.out.println(ans);
    }

    static int gcd (int num1, int num2) {
        if(num2  == 0) return num1;
        else return gcd(num2, num1%num2);
    }
} import java.util.Scanner; Scanner java.util public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int ans = 0;

        for (int i = 1; i < N+1; i++) {
            for (int j = 1; j < N+1; j++) {
                for (int k = 1; k <  N+1; k++) {
                        ans += gcd(gcd(i, j), k);
                }
            }
        }

        System.out.println(ans);
    }

    static int gcd (int num1, int num2) {
        if(num2  == 0) return num1;
        else return gcd(num2, num1%num2);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int ans = 0;

        for (int i = 1; i < N+1; i++) {
            for (int j = 1; j < N+1; j++) {
                for (int k = 1; k <  N+1; k++) {
                        ans += gcd(gcd(i, j), k);
                }
            }
        }

        System.out.println(ans);
    } main {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int ans = 0;

        for (int i = 1; i < N+1; i++) {
            for (int j = 1; j < N+1; j++) {
                for (int k = 1; k <  N+1; k++) {
                        ans += gcd(gcd(i, j), k);
                }
            }
        }

        System.out.println(ans);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc int ans = 0; ans 0 for (int i = 1; i < N+1; i++) {
            for (int j = 1; j < N+1; j++) {
                for (int k = 1; k <  N+1; k++) {
                        ans += gcd(gcd(i, j), k);
                }
            }
        } int i = 1; int i = 1; i 1 i < N+1 i N+1 N 1 i++ i++ i {
            for (int j = 1; j < N+1; j++) {
                for (int k = 1; k <  N+1; k++) {
                        ans += gcd(gcd(i, j), k);
                }
            }
        } {
            for (int j = 1; j < N+1; j++) {
                for (int k = 1; k <  N+1; k++) {
                        ans += gcd(gcd(i, j), k);
                }
            }
        } for (int j = 1; j < N+1; j++) {
                for (int k = 1; k <  N+1; k++) {
                        ans += gcd(gcd(i, j), k);
                }
            } int j = 1; int j = 1; j 1 j < N+1 j N+1 N 1 j++ j++ j {
                for (int k = 1; k <  N+1; k++) {
                        ans += gcd(gcd(i, j), k);
                }
            } {
                for (int k = 1; k <  N+1; k++) {
                        ans += gcd(gcd(i, j), k);
                }
            } for (int k = 1; k <  N+1; k++) {
                        ans += gcd(gcd(i, j), k);
                } int k = 1; int k = 1; k 1 k <  N+1 k N+1 N 1 k++ k++ k {
                        ans += gcd(gcd(i, j), k);
                } {
                        ans += gcd(gcd(i, j), k);
                } ans += gcd(gcd(i, j), k) ans gcd(gcd(i, j), k) gcd gcd(i, j) gcd i j k System.out.println(ans) System.out.println System.out System System.out ans String[] args args static int gcd (int num1, int num2) {
        if(num2  == 0) return num1;
        else return gcd(num2, num1%num2);
    } gcd {
        if(num2  == 0) return num1;
        else return gcd(num2, num1%num2);
    } if(num2  == 0) return num1;
        else return gcd(num2, num1%num2); num2  == 0 num2 0 return num1; num1 return gcd(num2, num1%num2); gcd(num2, num1%num2) gcd num2 num1%num2 num1 num2 int num1 num1 int num2 num2 public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int ans = 0;

        for (int i = 1; i < N+1; i++) {
            for (int j = 1; j < N+1; j++) {
                for (int k = 1; k <  N+1; k++) {
                        ans += gcd(gcd(i, j), k);
                }
            }
        }

        System.out.println(ans);
    }

    static int gcd (int num1, int num2) {
        if(num2  == 0) return num1;
        else return gcd(num2, num1%num2);
    }
} public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int ans = 0;

        for (int i = 1; i < N+1; i++) {
            for (int j = 1; j < N+1; j++) {
                for (int k = 1; k <  N+1; k++) {
                        ans += gcd(gcd(i, j), k);
                }
            }
        }

        System.out.println(ans);
    }

    static int gcd (int num1, int num2) {
        if(num2  == 0) return num1;
        else return gcd(num2, num1%num2);
    }
} Main
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int ans = 0;

        for (int i = 1; i < N+1; i++) {
            for (int j = 1; j < N+1; j++) {
                for (int k = 1; k <  N+1; k++) {
                        ans += gcd(gcd(i, j), k);
                }
            }
        }

        System.out.println(ans);
    }

    static int gcd (int num1, int num2) {
        if(num2  == 0) return num1;
        else return gcd(num2, num1%num2);
    }
}
