import java.util.*;

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

    }

    public static int gcd (int a, int b){
        int tmp = 0;
        if (a == 0) return b;
        if (b == 0) return a;
        if (a > b){
            tmp = a;
            a = b;
            b = tmp;
        }
        return gcd(b % a, a);
    }
} import java.util.*; util.* java public class Main {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        long sum = 0;
        int tmpGcd = 0;
        for (int i = 1; i <= K; i++){
            for (int j = 1; j <= K; j++){
                for (int k = 1; k <= K; k++){
                    tmpGcd = gcd(i, j);
                    sum += gcd(tmpGcd, k);
                }
            }
        }
        System.out.println(sum);

    }

    public static int gcd (int a, int b){
        int tmp = 0;
        if (a == 0) return b;
        if (b == 0) return a;
        if (a > b){
            tmp = a;
            a = b;
            b = tmp;
        }
        return gcd(b % a, a);
    }
} Main public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        long sum = 0;
        int tmpGcd = 0;
        for (int i = 1; i <= K; i++){
            for (int j = 1; j <= K; j++){
                for (int k = 1; k <= K; k++){
                    tmpGcd = gcd(i, j);
                    sum += gcd(tmpGcd, k);
                }
            }
        }
        System.out.println(sum);

    } main {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        long sum = 0;
        int tmpGcd = 0;
        for (int i = 1; i <= K; i++){
            for (int j = 1; j <= K; j++){
                for (int k = 1; k <= K; k++){
                    tmpGcd = gcd(i, j);
                    sum += gcd(tmpGcd, k);
                }
            }
        }
        System.out.println(sum);

    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc long sum = 0; sum 0 int tmpGcd = 0; tmpGcd 0 for (int i = 1; i <= K; i++){
            for (int j = 1; j <= K; j++){
                for (int k = 1; k <= K; k++){
                    tmpGcd = gcd(i, j);
                    sum += gcd(tmpGcd, 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++){
                    tmpGcd = gcd(i, j);
                    sum += gcd(tmpGcd, k);
                }
            }
        } {
            for (int j = 1; j <= K; j++){
                for (int k = 1; k <= K; k++){
                    tmpGcd = gcd(i, j);
                    sum += gcd(tmpGcd, k);
                }
            }
        } for (int j = 1; j <= K; j++){
                for (int k = 1; k <= K; k++){
                    tmpGcd = gcd(i, j);
                    sum += gcd(tmpGcd, k);
                }
            } int j = 1; int j = 1; j 1 j <= K j K j++ j++ j {
                for (int k = 1; k <= K; k++){
                    tmpGcd = gcd(i, j);
                    sum += gcd(tmpGcd, k);
                }
            } {
                for (int k = 1; k <= K; k++){
                    tmpGcd = gcd(i, j);
                    sum += gcd(tmpGcd, k);
                }
            } for (int k = 1; k <= K; k++){
                    tmpGcd = gcd(i, j);
                    sum += gcd(tmpGcd, k);
                } int k = 1; int k = 1; k 1 k <= K k K k++ k++ k {
                    tmpGcd = gcd(i, j);
                    sum += gcd(tmpGcd, k);
                } {
                    tmpGcd = gcd(i, j);
                    sum += gcd(tmpGcd, k);
                } tmpGcd = gcd(i, j) tmpGcd gcd(i, j) gcd i j sum += gcd(tmpGcd, k) sum gcd(tmpGcd, k) gcd tmpGcd k System.out.println(sum) System.out.println System.out System System.out sum String[] args args public static int gcd (int a, int b){
        int tmp = 0;
        if (a == 0) return b;
        if (b == 0) return a;
        if (a > b){
            tmp = a;
            a = b;
            b = tmp;
        }
        return gcd(b % a, a);
    } gcd {
        int tmp = 0;
        if (a == 0) return b;
        if (b == 0) return a;
        if (a > b){
            tmp = a;
            a = b;
            b = tmp;
        }
        return gcd(b % a, a);
    } int tmp = 0; tmp 0 if (a == 0) return b; a == 0 a 0 return b; b if (b == 0) return a; b == 0 b 0 return a; a if (a > b){
            tmp = a;
            a = b;
            b = tmp;
        } a > b a b {
            tmp = a;
            a = b;
            b = tmp;
        } tmp = a tmp a a = b a b b = tmp b tmp return gcd(b % a, a); gcd(b % a, a) gcd b % a b a a int a a int b b public class Main {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        long sum = 0;
        int tmpGcd = 0;
        for (int i = 1; i <= K; i++){
            for (int j = 1; j <= K; j++){
                for (int k = 1; k <= K; k++){
                    tmpGcd = gcd(i, j);
                    sum += gcd(tmpGcd, k);
                }
            }
        }
        System.out.println(sum);

    }

    public static int gcd (int a, int b){
        int tmp = 0;
        if (a == 0) return b;
        if (b == 0) return a;
        if (a > b){
            tmp = a;
            a = b;
            b = tmp;
        }
        return gcd(b % a, a);
    }
} public class Main {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        long sum = 0;
        int tmpGcd = 0;
        for (int i = 1; i <= K; i++){
            for (int j = 1; j <= K; j++){
                for (int k = 1; k <= K; k++){
                    tmpGcd = gcd(i, j);
                    sum += gcd(tmpGcd, k);
                }
            }
        }
        System.out.println(sum);

    }

    public static int gcd (int a, int b){
        int tmp = 0;
        if (a == 0) return b;
        if (b == 0) return a;
        if (a > b){
            tmp = a;
            a = b;
            b = tmp;
        }
        return gcd(b % a, a);
    }
} Main
import java.util.*;

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

    }

    public static int gcd (int a, int b){
        int tmp = 0;
        if (a == 0) return b;
        if (b == 0) return a;
        if (a > b){
            tmp = a;
            a = b;
            b = tmp;
        }
        return gcd(b % a, a);
    }
}