import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int sum = 0;
        for(int a = 1; a <= k; a++){
            for(int b = 1; b <= k; b++){
                for(int c = 1; c <= k; c++){
                    sum += gcd(gcd(a, b),c);
                }
            }
        }
        System.out.println(sum);
    }
    public static int gcd(int m, int n) {
        int r;
        while (n > 0) {
            r = m % n;
            m = n;
            n = r;
        }
        return m;
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int sum = 0;
        for(int a = 1; a <= k; a++){
            for(int b = 1; b <= k; b++){
                for(int c = 1; c <= k; c++){
                    sum += gcd(gcd(a, b),c);
                }
            }
        }
        System.out.println(sum);
    }
    public static int gcd(int m, int n) {
        int r;
        while (n > 0) {
            r = m % n;
            m = n;
            n = r;
        }
        return m;
    }
} Main public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int sum = 0;
        for(int a = 1; a <= k; a++){
            for(int b = 1; b <= k; b++){
                for(int c = 1; c <= k; c++){
                    sum += gcd(gcd(a, b),c);
                }
            }
        }
        System.out.println(sum);
    } main {
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int sum = 0;
        for(int a = 1; a <= k; a++){
            for(int b = 1; b <= k; b++){
                for(int c = 1; c <= k; c++){
                    sum += gcd(gcd(a, b),c);
                }
            }
        }
        System.out.println(sum);
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int k = scan.nextInt(); k scan.nextInt() scan.nextInt scan int sum = 0; sum 0 for(int a = 1; a <= k; a++){
            for(int b = 1; b <= k; b++){
                for(int c = 1; c <= k; c++){
                    sum += gcd(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++){
                    sum += gcd(gcd(a, b),c);
                }
            }
        } {
            for(int b = 1; b <= k; b++){
                for(int c = 1; c <= k; c++){
                    sum += gcd(gcd(a, b),c);
                }
            }
        } for(int b = 1; b <= k; b++){
                for(int c = 1; c <= k; c++){
                    sum += gcd(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++){
                    sum += gcd(gcd(a, b),c);
                }
            } {
                for(int c = 1; c <= k; c++){
                    sum += gcd(gcd(a, b),c);
                }
            } for(int c = 1; c <= k; c++){
                    sum += gcd(gcd(a, b),c);
                } int c = 1; int c = 1; c 1 c <= k c k c++ c++ c {
                    sum += gcd(gcd(a, b),c);
                } {
                    sum += gcd(gcd(a, b),c);
                } sum += gcd(gcd(a, b),c) sum gcd(gcd(a, b),c) gcd gcd(a, b) gcd a b c System.out.println(sum) System.out.println System.out System System.out sum String[] args args public static int gcd(int m, int n) {
        int r;
        while (n > 0) {
            r = m % n;
            m = n;
            n = r;
        }
        return m;
    } gcd {
        int r;
        while (n > 0) {
            r = m % n;
            m = n;
            n = r;
        }
        return m;
    } int r; r while (n > 0) {
            r = m % n;
            m = n;
            n = r;
        } n > 0 n 0 {
            r = m % n;
            m = n;
            n = r;
        } r = m % n r m % n m n m = n m n n = r n r return m; m int m m int n n public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int sum = 0;
        for(int a = 1; a <= k; a++){
            for(int b = 1; b <= k; b++){
                for(int c = 1; c <= k; c++){
                    sum += gcd(gcd(a, b),c);
                }
            }
        }
        System.out.println(sum);
    }
    public static int gcd(int m, int n) {
        int r;
        while (n > 0) {
            r = m % n;
            m = n;
            n = r;
        }
        return m;
    }
} public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int sum = 0;
        for(int a = 1; a <= k; a++){
            for(int b = 1; b <= k; b++){
                for(int c = 1; c <= k; c++){
                    sum += gcd(gcd(a, b),c);
                }
            }
        }
        System.out.println(sum);
    }
    public static int gcd(int m, int n) {
        int r;
        while (n > 0) {
            r = m % n;
            m = n;
            n = r;
        }
        return m;
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int sum = 0;
        for(int a = 1; a <= k; a++){
            for(int b = 1; b <= k; b++){
                for(int c = 1; c <= k; c++){
                    sum += gcd(gcd(a, b),c);
                }
            }
        }
        System.out.println(sum);
    }
    public static int gcd(int m, int n) {
        int r;
        while (n > 0) {
            r = m % n;
            m = n;
            n = r;
        }
        return m;
    }
}