import java.util.*;

public class Main {
    public static long gcd(long a, long b){
        long x;
        long y;
        if(b > a){
            x = b;
            y = a;
        }else{
            x = a;
            y = b;
        }
        long lm;
        while((lm = x%y) != 0){
            x = y;
            y = lm;
        }
        return y;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long sum = 0;
        for(long a = 1; a <= N; a++){
            for(long b = 1; b <= N; b++){
                for(long c = 1; c <= N; c++){
                    sum = sum + gcd(a, gcd(b, c));
                }
            }
        }
        System.out.println(sum);
    }
}  import java.util.*; util.* java public class Main {
    public static long gcd(long a, long b){
        long x;
        long y;
        if(b > a){
            x = b;
            y = a;
        }else{
            x = a;
            y = b;
        }
        long lm;
        while((lm = x%y) != 0){
            x = y;
            y = lm;
        }
        return y;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long sum = 0;
        for(long a = 1; a <= N; a++){
            for(long b = 1; b <= N; b++){
                for(long c = 1; c <= N; c++){
                    sum = sum + gcd(a, gcd(b, c));
                }
            }
        }
        System.out.println(sum);
    }
} Main public static long gcd(long a, long b){
        long x;
        long y;
        if(b > a){
            x = b;
            y = a;
        }else{
            x = a;
            y = b;
        }
        long lm;
        while((lm = x%y) != 0){
            x = y;
            y = lm;
        }
        return y;
    } gcd {
        long x;
        long y;
        if(b > a){
            x = b;
            y = a;
        }else{
            x = a;
            y = b;
        }
        long lm;
        while((lm = x%y) != 0){
            x = y;
            y = lm;
        }
        return y;
    } long x; x long y; y if(b > a){
            x = b;
            y = a;
        }else{
            x = a;
            y = b;
        } b > a b a {
            x = b;
            y = a;
        } x = b x b y = a y a {
            x = a;
            y = b;
        } x = a x a y = b y b long lm; lm while((lm = x%y) != 0){
            x = y;
            y = lm;
        } (lm = x%y) != 0 (lm = x%y) lm x%y x y 0 {
            x = y;
            y = lm;
        } x = y x y y = lm y lm return y; y long a a long b b public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long sum = 0;
        for(long a = 1; a <= N; a++){
            for(long b = 1; b <= N; b++){
                for(long c = 1; c <= N; c++){
                    sum = sum + gcd(a, gcd(b, c));
                }
            }
        }
        System.out.println(sum);
    } main {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long sum = 0;
        for(long a = 1; a <= N; a++){
            for(long b = 1; b <= N; b++){
                for(long c = 1; c <= N; c++){
                    sum = sum + gcd(a, gcd(b, c));
                }
            }
        }
        System.out.println(sum);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long N = sc.nextLong(); N sc.nextLong() sc.nextLong sc long sum = 0; sum 0 for(long a = 1; a <= N; a++){
            for(long b = 1; b <= N; b++){
                for(long c = 1; c <= N; c++){
                    sum = sum + gcd(a, gcd(b, c));
                }
            }
        } long a = 1; long a = 1; a 1 a <= N a N a++ a++ a {
            for(long b = 1; b <= N; b++){
                for(long c = 1; c <= N; c++){
                    sum = sum + gcd(a, gcd(b, c));
                }
            }
        } {
            for(long b = 1; b <= N; b++){
                for(long c = 1; c <= N; c++){
                    sum = sum + gcd(a, gcd(b, c));
                }
            }
        } for(long b = 1; b <= N; b++){
                for(long c = 1; c <= N; c++){
                    sum = sum + gcd(a, gcd(b, c));
                }
            } long b = 1; long b = 1; b 1 b <= N b N b++ b++ b {
                for(long c = 1; c <= N; c++){
                    sum = sum + gcd(a, gcd(b, c));
                }
            } {
                for(long c = 1; c <= N; c++){
                    sum = sum + gcd(a, gcd(b, c));
                }
            } for(long c = 1; c <= N; c++){
                    sum = sum + gcd(a, gcd(b, c));
                } long c = 1; long c = 1; c 1 c <= N c N c++ c++ c {
                    sum = sum + gcd(a, gcd(b, c));
                } {
                    sum = sum + gcd(a, gcd(b, c));
                } sum = sum + gcd(a, gcd(b, c)) sum sum + gcd(a, gcd(b, c)) sum gcd(a, gcd(b, c)) gcd a gcd(b, c) gcd b c System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main {
    public static long gcd(long a, long b){
        long x;
        long y;
        if(b > a){
            x = b;
            y = a;
        }else{
            x = a;
            y = b;
        }
        long lm;
        while((lm = x%y) != 0){
            x = y;
            y = lm;
        }
        return y;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long sum = 0;
        for(long a = 1; a <= N; a++){
            for(long b = 1; b <= N; b++){
                for(long c = 1; c <= N; c++){
                    sum = sum + gcd(a, gcd(b, c));
                }
            }
        }
        System.out.println(sum);
    }
} public class Main {
    public static long gcd(long a, long b){
        long x;
        long y;
        if(b > a){
            x = b;
            y = a;
        }else{
            x = a;
            y = b;
        }
        long lm;
        while((lm = x%y) != 0){
            x = y;
            y = lm;
        }
        return y;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long sum = 0;
        for(long a = 1; a <= N; a++){
            for(long b = 1; b <= N; b++){
                for(long c = 1; c <= N; c++){
                    sum = sum + gcd(a, gcd(b, c));
                }
            }
        }
        System.out.println(sum);
    }
} Main
import java.util.*;

public class Main {
    public static long gcd(long a, long b){
        long x;
        long y;
        if(b > a){
            x = b;
            y = a;
        }else{
            x = a;
            y = b;
        }
        long lm;
        while((lm = x%y) != 0){
            x = y;
            y = lm;
        }
        return y;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long sum = 0;
        for(long a = 1; a <= N; a++){
            for(long b = 1; b <= N; b++){
                for(long c = 1; c <= N; c++){
                    sum = sum + gcd(a, gcd(b, c));
                }
            }
        }
        System.out.println(sum);
    }
} 