import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int k=sc.nextInt();
        long ans=0;


        for(int i=1;i<=k;i++){
            for(int j=1;j<=k;j++){
                for(int l=1;l<=k;l++){
                    ans+=helper(i,j,l);
                }
            }
        }
        System.out.println(ans);
    }

    private static long helper(int i, int j, int k) {

        long temp=gcd(i,j);
        long ans=gcd(temp,k);
        return ans;
    }

    private static long gcd(long a, long b) {
        if(b==0) return a;
        else
            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 k=sc.nextInt();
        long ans=0;


        for(int i=1;i<=k;i++){
            for(int j=1;j<=k;j++){
                for(int l=1;l<=k;l++){
                    ans+=helper(i,j,l);
                }
            }
        }
        System.out.println(ans);
    }

    private static long helper(int i, int j, int k) {

        long temp=gcd(i,j);
        long ans=gcd(temp,k);
        return ans;
    }

    private static long gcd(long a, long b) {
        if(b==0) return a;
        else
            return gcd(b,a%b);
    }
} Main public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int k=sc.nextInt();
        long ans=0;


        for(int i=1;i<=k;i++){
            for(int j=1;j<=k;j++){
                for(int l=1;l<=k;l++){
                    ans+=helper(i,j,l);
                }
            }
        }
        System.out.println(ans);
    } main {
        Scanner sc=new Scanner(System.in);
        int k=sc.nextInt();
        long ans=0;


        for(int i=1;i<=k;i++){
            for(int j=1;j<=k;j++){
                for(int l=1;l<=k;l++){
                    ans+=helper(i,j,l);
                }
            }
        }
        System.out.println(ans);
    } Scanner sc=new Scanner(System.in); sc new Scanner(System.in) int k=sc.nextInt(); k sc.nextInt() sc.nextInt sc long ans=0; ans 0 for(int i=1;i<=k;i++){
            for(int j=1;j<=k;j++){
                for(int l=1;l<=k;l++){
                    ans+=helper(i,j,l);
                }
            }
        } int i=1; int i=1; i 1 i<=k i k i++ i++ i {
            for(int j=1;j<=k;j++){
                for(int l=1;l<=k;l++){
                    ans+=helper(i,j,l);
                }
            }
        } {
            for(int j=1;j<=k;j++){
                for(int l=1;l<=k;l++){
                    ans+=helper(i,j,l);
                }
            }
        } for(int j=1;j<=k;j++){
                for(int l=1;l<=k;l++){
                    ans+=helper(i,j,l);
                }
            } int j=1; int j=1; j 1 j<=k j k j++ j++ j {
                for(int l=1;l<=k;l++){
                    ans+=helper(i,j,l);
                }
            } {
                for(int l=1;l<=k;l++){
                    ans+=helper(i,j,l);
                }
            } for(int l=1;l<=k;l++){
                    ans+=helper(i,j,l);
                } int l=1; int l=1; l 1 l<=k l k l++ l++ l {
                    ans+=helper(i,j,l);
                } {
                    ans+=helper(i,j,l);
                } ans+=helper(i,j,l) ans helper(i,j,l) helper i j l System.out.println(ans) System.out.println System.out System System.out ans String[] args args private static long helper(int i, int j, int k) {

        long temp=gcd(i,j);
        long ans=gcd(temp,k);
        return ans;
    } helper {

        long temp=gcd(i,j);
        long ans=gcd(temp,k);
        return ans;
    } long temp=gcd(i,j); temp gcd(i,j) gcd i j long ans=gcd(temp,k); ans gcd(temp,k) gcd temp k return ans; ans int i i int j j int k k private static long gcd(long a, long b) {
        if(b==0) return a;
        else
            return gcd(b,a%b);
    } gcd {
        if(b==0) return a;
        else
            return gcd(b,a%b);
    } if(b==0) return a;
        else
            return gcd(b,a%b); b==0 b 0 return a; a return gcd(b,a%b); gcd(b,a%b) gcd b a%b a b long a a long b b public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int k=sc.nextInt();
        long ans=0;


        for(int i=1;i<=k;i++){
            for(int j=1;j<=k;j++){
                for(int l=1;l<=k;l++){
                    ans+=helper(i,j,l);
                }
            }
        }
        System.out.println(ans);
    }

    private static long helper(int i, int j, int k) {

        long temp=gcd(i,j);
        long ans=gcd(temp,k);
        return ans;
    }

    private static long gcd(long a, long b) {
        if(b==0) return a;
        else
            return gcd(b,a%b);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int k=sc.nextInt();
        long ans=0;


        for(int i=1;i<=k;i++){
            for(int j=1;j<=k;j++){
                for(int l=1;l<=k;l++){
                    ans+=helper(i,j,l);
                }
            }
        }
        System.out.println(ans);
    }

    private static long helper(int i, int j, int k) {

        long temp=gcd(i,j);
        long ans=gcd(temp,k);
        return ans;
    }

    private static long gcd(long a, long b) {
        if(b==0) return a;
        else
            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 k=sc.nextInt();
        long ans=0;


        for(int i=1;i<=k;i++){
            for(int j=1;j<=k;j++){
                for(int l=1;l<=k;l++){
                    ans+=helper(i,j,l);
                }
            }
        }
        System.out.println(ans);
    }

    private static long helper(int i, int j, int k) {

        long temp=gcd(i,j);
        long ans=gcd(temp,k);
        return ans;
    }

    private static long gcd(long a, long b) {
        if(b==0) return a;
        else
            return gcd(b,a%b);
    }
}
