import java.util.*;

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

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

    }
    static int gcd(int a, int b){
        if(a%b==0) return b;
        return gcd(b,a%b);
    }
} Main public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int ans = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                for(int k = 1; k <= n; k++){
                    ans += gcd(gcd(i,j),k);
                }
            }
        }
        System.out.print(ans);

    } main {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int ans = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                for(int k = 1; k <= n; k++){
                    ans += gcd(gcd(i,j),k);
                }
            }
        }
        System.out.print(ans);

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

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

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

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

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