import java.util.*;

public class Main {
    public static int gcd(int a, int b) {
        if(a < b) gcd(b, a);
        if(b == 0) return a;
        return gcd(b,a%b);
    }

    public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Integer K = Integer.parseInt(scanner.next());
        Integer total = 0;

        for (int i = 1; i <= K; i++) {
            for (int j = 1; j <= K; j++) {
                for (int k = 1; k <= K; k++) {
                    total += gcd(i, gcd(j,k));
                }
            }
        }
        System.out.println(total);
    }
} import java.util.*; util.* java public class Main {
    public static int gcd(int a, int b) {
        if(a < b) gcd(b, a);
        if(b == 0) return a;
        return gcd(b,a%b);
    }

    public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Integer K = Integer.parseInt(scanner.next());
        Integer total = 0;

        for (int i = 1; i <= K; i++) {
            for (int j = 1; j <= K; j++) {
                for (int k = 1; k <= K; k++) {
                    total += gcd(i, gcd(j,k));
                }
            }
        }
        System.out.println(total);
    }
} Main public static int gcd(int a, int b) {
        if(a < b) gcd(b, a);
        if(b == 0) return a;
        return gcd(b,a%b);
    } gcd {
        if(a < b) gcd(b, a);
        if(b == 0) return a;
        return gcd(b,a%b);
    } if(a < b) gcd(b, a); a < b a b gcd(b, a) gcd b a if(b == 0) return a; b == 0 b 0 return a; a return gcd(b,a%b); gcd(b,a%b) gcd b a%b a b int a a int b b public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Integer K = Integer.parseInt(scanner.next());
        Integer total = 0;

        for (int i = 1; i <= K; i++) {
            for (int j = 1; j <= K; j++) {
                for (int k = 1; k <= K; k++) {
                    total += gcd(i, gcd(j,k));
                }
            }
        }
        System.out.println(total);
    } main {
        //コード
        Scanner scanner = new Scanner(System.in);
        Integer K = Integer.parseInt(scanner.next());
        Integer total = 0;

        for (int i = 1; i <= K; i++) {
            for (int j = 1; j <= K; j++) {
                for (int k = 1; k <= K; k++) {
                    total += gcd(i, gcd(j,k));
                }
            }
        }
        System.out.println(total);
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) Integer K = Integer.parseInt(scanner.next()); K Integer.parseInt(scanner.next()) Integer.parseInt Integer scanner.next() scanner.next scanner Integer total = 0; total 0 for (int i = 1; i <= K; i++) {
            for (int j = 1; j <= K; j++) {
                for (int k = 1; k <= K; k++) {
                    total += gcd(i, gcd(j,k));
                }
            }
        } int i = 1; int i = 1; i 1 i <= K i K i++ i++ i {
            for (int j = 1; j <= K; j++) {
                for (int k = 1; k <= K; k++) {
                    total += gcd(i, gcd(j,k));
                }
            }
        } {
            for (int j = 1; j <= K; j++) {
                for (int k = 1; k <= K; k++) {
                    total += gcd(i, gcd(j,k));
                }
            }
        } for (int j = 1; j <= K; j++) {
                for (int k = 1; k <= K; k++) {
                    total += gcd(i, gcd(j,k));
                }
            } int j = 1; int j = 1; j 1 j <= K j K j++ j++ j {
                for (int k = 1; k <= K; k++) {
                    total += gcd(i, gcd(j,k));
                }
            } {
                for (int k = 1; k <= K; k++) {
                    total += gcd(i, gcd(j,k));
                }
            } for (int k = 1; k <= K; k++) {
                    total += gcd(i, gcd(j,k));
                } int k = 1; int k = 1; k 1 k <= K k K k++ k++ k {
                    total += gcd(i, gcd(j,k));
                } {
                    total += gcd(i, gcd(j,k));
                } total += gcd(i, gcd(j,k)) total gcd(i, gcd(j,k)) gcd i gcd(j,k) gcd j k System.out.println(total) System.out.println System.out System System.out total String[] args args public class Main {
    public static int gcd(int a, int b) {
        if(a < b) gcd(b, a);
        if(b == 0) return a;
        return gcd(b,a%b);
    }

    public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Integer K = Integer.parseInt(scanner.next());
        Integer total = 0;

        for (int i = 1; i <= K; i++) {
            for (int j = 1; j <= K; j++) {
                for (int k = 1; k <= K; k++) {
                    total += gcd(i, gcd(j,k));
                }
            }
        }
        System.out.println(total);
    }
} public class Main {
    public static int gcd(int a, int b) {
        if(a < b) gcd(b, a);
        if(b == 0) return a;
        return gcd(b,a%b);
    }

    public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Integer K = Integer.parseInt(scanner.next());
        Integer total = 0;

        for (int i = 1; i <= K; i++) {
            for (int j = 1; j <= K; j++) {
                for (int k = 1; k <= K; k++) {
                    total += gcd(i, gcd(j,k));
                }
            }
        }
        System.out.println(total);
    }
} Main
import java.util.*;

public class Main {
    public static int gcd(int a, int b) {
        if(a < b) gcd(b, a);
        if(b == 0) return a;
        return gcd(b,a%b);
    }

    public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Integer K = Integer.parseInt(scanner.next());
        Integer total = 0;

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