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