import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int K;
        int a,b,c;
        try (Scanner sc = new Scanner(System.in)) {
            K = sc.nextInt();
        }
        a = b = c =  K;
        long result = 0;
        for (int i = 1; i <= a; i++) {
            for (int j = 1; j <= b; j++) {
                for (int k = 1; k <= c; k++) {
                    long temp = gcd((long)i,(long)j);
                    temp = gcd(temp,(long)k);
                    result += temp;
                }
            }
        }
        System.out.println(result);
    }

    //最大公約数
    static long gcd(long a, long b) {
        long temp;
        while ((temp = a % b) != 0) {
            a = b;
            b = temp;
        }
        return (long) b;
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        int K;
        int a,b,c;
        try (Scanner sc = new Scanner(System.in)) {
            K = sc.nextInt();
        }
        a = b = c =  K;
        long result = 0;
        for (int i = 1; i <= a; i++) {
            for (int j = 1; j <= b; j++) {
                for (int k = 1; k <= c; k++) {
                    long temp = gcd((long)i,(long)j);
                    temp = gcd(temp,(long)k);
                    result += temp;
                }
            }
        }
        System.out.println(result);
    }

    //最大公約数
    static long gcd(long a, long b) {
        long temp;
        while ((temp = a % b) != 0) {
            a = b;
            b = temp;
        }
        return (long) b;
    }
} Main public static void main(String[] args) {
        int K;
        int a,b,c;
        try (Scanner sc = new Scanner(System.in)) {
            K = sc.nextInt();
        }
        a = b = c =  K;
        long result = 0;
        for (int i = 1; i <= a; i++) {
            for (int j = 1; j <= b; j++) {
                for (int k = 1; k <= c; k++) {
                    long temp = gcd((long)i,(long)j);
                    temp = gcd(temp,(long)k);
                    result += temp;
                }
            }
        }
        System.out.println(result);
    } main {
        int K;
        int a,b,c;
        try (Scanner sc = new Scanner(System.in)) {
            K = sc.nextInt();
        }
        a = b = c =  K;
        long result = 0;
        for (int i = 1; i <= a; i++) {
            for (int j = 1; j <= b; j++) {
                for (int k = 1; k <= c; k++) {
                    long temp = gcd((long)i,(long)j);
                    temp = gcd(temp,(long)k);
                    result += temp;
                }
            }
        }
        System.out.println(result);
    } int K; K int a a b b c; c try (Scanner sc = new Scanner(System.in)) {
            K = sc.nextInt();
        } {
            K = sc.nextInt();
        } Scanner sc = new Scanner(System.in) Scanner sc = new Scanner(System.in) new Scanner(System.in) K = sc.nextInt() K sc.nextInt() sc.nextInt sc a = b = c =  K a b = c =  K b c =  K c K long result = 0; result 0 for (int i = 1; i <= a; i++) {
            for (int j = 1; j <= b; j++) {
                for (int k = 1; k <= c; k++) {
                    long temp = gcd((long)i,(long)j);
                    temp = gcd(temp,(long)k);
                    result += temp;
                }
            }
        } int i = 1; int i = 1; i 1 i <= a i a i++ i++ i {
            for (int j = 1; j <= b; j++) {
                for (int k = 1; k <= c; k++) {
                    long temp = gcd((long)i,(long)j);
                    temp = gcd(temp,(long)k);
                    result += temp;
                }
            }
        } {
            for (int j = 1; j <= b; j++) {
                for (int k = 1; k <= c; k++) {
                    long temp = gcd((long)i,(long)j);
                    temp = gcd(temp,(long)k);
                    result += temp;
                }
            }
        } for (int j = 1; j <= b; j++) {
                for (int k = 1; k <= c; k++) {
                    long temp = gcd((long)i,(long)j);
                    temp = gcd(temp,(long)k);
                    result += temp;
                }
            } int j = 1; int j = 1; j 1 j <= b j b j++ j++ j {
                for (int k = 1; k <= c; k++) {
                    long temp = gcd((long)i,(long)j);
                    temp = gcd(temp,(long)k);
                    result += temp;
                }
            } {
                for (int k = 1; k <= c; k++) {
                    long temp = gcd((long)i,(long)j);
                    temp = gcd(temp,(long)k);
                    result += temp;
                }
            } for (int k = 1; k <= c; k++) {
                    long temp = gcd((long)i,(long)j);
                    temp = gcd(temp,(long)k);
                    result += temp;
                } int k = 1; int k = 1; k 1 k <= c k c k++ k++ k {
                    long temp = gcd((long)i,(long)j);
                    temp = gcd(temp,(long)k);
                    result += temp;
                } {
                    long temp = gcd((long)i,(long)j);
                    temp = gcd(temp,(long)k);
                    result += temp;
                } long temp = gcd((long)i,(long)j); temp gcd((long)i,(long)j) gcd (long)i (long)j temp = gcd(temp,(long)k) temp gcd(temp,(long)k) gcd temp (long)k result += temp result temp System.out.println(result) System.out.println System.out System System.out result String[] args args //最大公約数
    static long gcd(long a, long b) {
        long temp;
        while ((temp = a % b) != 0) {
            a = b;
            b = temp;
        }
        return (long) b;
    } gcd {
        long temp;
        while ((temp = a % b) != 0) {
            a = b;
            b = temp;
        }
        return (long) b;
    } long temp; temp while ((temp = a % b) != 0) {
            a = b;
            b = temp;
        } (temp = a % b) != 0 (temp = a % b) temp a % b a b 0 {
            a = b;
            b = temp;
        } a = b a b b = temp b temp return (long) b; (long) b long a a long b b public class Main {
    public static void main(String[] args) {
        int K;
        int a,b,c;
        try (Scanner sc = new Scanner(System.in)) {
            K = sc.nextInt();
        }
        a = b = c =  K;
        long result = 0;
        for (int i = 1; i <= a; i++) {
            for (int j = 1; j <= b; j++) {
                for (int k = 1; k <= c; k++) {
                    long temp = gcd((long)i,(long)j);
                    temp = gcd(temp,(long)k);
                    result += temp;
                }
            }
        }
        System.out.println(result);
    }

    //最大公約数
    static long gcd(long a, long b) {
        long temp;
        while ((temp = a % b) != 0) {
            a = b;
            b = temp;
        }
        return (long) b;
    }
} public class Main {
    public static void main(String[] args) {
        int K;
        int a,b,c;
        try (Scanner sc = new Scanner(System.in)) {
            K = sc.nextInt();
        }
        a = b = c =  K;
        long result = 0;
        for (int i = 1; i <= a; i++) {
            for (int j = 1; j <= b; j++) {
                for (int k = 1; k <= c; k++) {
                    long temp = gcd((long)i,(long)j);
                    temp = gcd(temp,(long)k);
                    result += temp;
                }
            }
        }
        System.out.println(result);
    }

    //最大公約数
    static long gcd(long a, long b) {
        long temp;
        while ((temp = a % b) != 0) {
            a = b;
            b = temp;
        }
        return (long) b;
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int K;
        int a,b,c;
        try (Scanner sc = new Scanner(System.in)) {
            K = sc.nextInt();
        }
        a = b = c =  K;
        long result = 0;
        for (int i = 1; i <= a; i++) {
            for (int j = 1; j <= b; j++) {
                for (int k = 1; k <= c; k++) {
                    long temp = gcd((long)i,(long)j);
                    temp = gcd(temp,(long)k);
                    result += temp;
                }
            }
        }
        System.out.println(result);
    }

    //最大公約数
    static long gcd(long a, long b) {
        long temp;
        while ((temp = a % b) != 0) {
            a = b;
            b = temp;
        }
        return (long) b;
    }
}
