import java.util.*;

class Main{

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

        System.out.println(ans);
    }

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

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

        System.out.println(ans);
    }

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

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

        System.out.println(ans);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc long ans = 0; ans 0 for(int i = 1;i <= n;i++){
            for(int j = 1;j <= n;j++){
                int t = gcd(i,j);
                for(int k = 1;k <= n;k++){
                    ans += gcd(t,k);
                }
            }
        } int i = 1; int i = 1; i 1 i <= n i n i++ i++ i {
            for(int j = 1;j <= n;j++){
                int t = gcd(i,j);
                for(int k = 1;k <= n;k++){
                    ans += gcd(t,k);
                }
            }
        } {
            for(int j = 1;j <= n;j++){
                int t = gcd(i,j);
                for(int k = 1;k <= n;k++){
                    ans += gcd(t,k);
                }
            }
        } for(int j = 1;j <= n;j++){
                int t = gcd(i,j);
                for(int k = 1;k <= n;k++){
                    ans += gcd(t,k);
                }
            } int j = 1; int j = 1; j 1 j <= n j n j++ j++ j {
                int t = gcd(i,j);
                for(int k = 1;k <= n;k++){
                    ans += gcd(t,k);
                }
            } {
                int t = gcd(i,j);
                for(int k = 1;k <= n;k++){
                    ans += gcd(t,k);
                }
            } int t = gcd(i,j); t gcd(i,j) gcd i j for(int k = 1;k <= n;k++){
                    ans += gcd(t,k);
                } int k = 1; int k = 1; k 1 k <= n k n k++ k++ k {
                    ans += gcd(t,k);
                } {
                    ans += gcd(t,k);
                } ans += gcd(t,k) ans gcd(t,k) gcd t k System.out.println(ans) System.out.println System.out System System.out ans String[] args args static int gcd(int a,int b){
        return b==0? a:gcd(b,a%b);
    } gcd {
        return b==0? a:gcd(b,a%b);
    } return b==0? a:gcd(b,a%b); b==0? a:gcd(b,a%b) b==0 b 0 a gcd(b,a%b) gcd b a%b a b int a a int b b
import java.util.*;

class Main{

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

        System.out.println(ans);
    }

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