
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // write your code here

        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int K = in.nextInt();
        long ans = 0;

//        ans  = gcd(1,2);

        for (int i = 1; i <= K; i++) {
            for (int j = 1 ; j <=K; j++) {
                for (int k = 1; k <= K; k++) {
                    ans += gcd(i,j,k);
                }
            }
        }

        out.println(ans);

        in.close();
        out.close();
    }

    public static int gcd(int a, int b, int c){
        return gcd(gcd(a,b),c);
    }

    public static int gcd(int a, int b){
        if(b == 0) return a;
        return gcd(b,a%b);
    }
} import java.io.PrintWriter; PrintWriter java.io import java.util.Scanner; Scanner java.util public class Main {

    public static void main(String[] args) {
        // write your code here

        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int K = in.nextInt();
        long ans = 0;

//        ans  = gcd(1,2);

        for (int i = 1; i <= K; i++) {
            for (int j = 1 ; j <=K; j++) {
                for (int k = 1; k <= K; k++) {
                    ans += gcd(i,j,k);
                }
            }
        }

        out.println(ans);

        in.close();
        out.close();
    }

    public static int gcd(int a, int b, int c){
        return gcd(gcd(a,b),c);
    }

    public static int gcd(int a, int b){
        if(b == 0) return a;
        return gcd(b,a%b);
    }
} Main public static void main(String[] args) {
        // write your code here

        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int K = in.nextInt();
        long ans = 0;

//        ans  = gcd(1,2);

        for (int i = 1; i <= K; i++) {
            for (int j = 1 ; j <=K; j++) {
                for (int k = 1; k <= K; k++) {
                    ans += gcd(i,j,k);
                }
            }
        }

        out.println(ans);

        in.close();
        out.close();
    } main {
        // write your code here

        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int K = in.nextInt();
        long ans = 0;

//        ans  = gcd(1,2);

        for (int i = 1; i <= K; i++) {
            for (int j = 1 ; j <=K; j++) {
                for (int k = 1; k <= K; k++) {
                    ans += gcd(i,j,k);
                }
            }
        }

        out.println(ans);

        in.close();
        out.close();
    } Scanner in = new Scanner(System.in); in new Scanner(System.in) PrintWriter out = new PrintWriter(System.out); out new PrintWriter(System.out) int K = in.nextInt(); K in.nextInt() in.nextInt in long ans = 0; ans 0 for (int i = 1; i <= K; i++) {
            for (int j = 1 ; j <=K; j++) {
                for (int k = 1; k <= K; k++) {
                    ans += gcd(i,j,k);
                }
            }
        } int i = 1; int i = 1; i 1 i <= K i K i++ i++ i {
            for (int j = 1 ; j <=K; j++) {
                for (int k = 1; k <= K; k++) {
                    ans += gcd(i,j,k);
                }
            }
        } {
            for (int j = 1 ; j <=K; j++) {
                for (int k = 1; k <= K; k++) {
                    ans += gcd(i,j,k);
                }
            }
        } for (int j = 1 ; j <=K; j++) {
                for (int k = 1; k <= K; k++) {
                    ans += gcd(i,j,k);
                }
            } int j = 1 ; int j = 1 ; j 1 j <=K j K j++ j++ j {
                for (int k = 1; k <= K; k++) {
                    ans += gcd(i,j,k);
                }
            } {
                for (int k = 1; k <= K; k++) {
                    ans += gcd(i,j,k);
                }
            } for (int k = 1; k <= K; k++) {
                    ans += gcd(i,j,k);
                } int k = 1; int k = 1; k 1 k <= K k K k++ k++ k {
                    ans += gcd(i,j,k);
                } {
                    ans += gcd(i,j,k);
                } ans += gcd(i,j,k) ans gcd(i,j,k) gcd i j k out.println(ans) out.println out ans in.close() in.close in out.close() out.close out String[] args args public static int gcd(int a, int b, int c){
        return gcd(gcd(a,b),c);
    } gcd {
        return gcd(gcd(a,b),c);
    } return gcd(gcd(a,b),c); gcd(gcd(a,b),c) gcd gcd(a,b) gcd a b c int a a int b b int c c public static int gcd(int a, int b){
        if(b == 0) return a;
        return gcd(b,a%b);
    } gcd {
        if(b == 0) return a;
        return gcd(b,a%b);
    } 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 class Main {

    public static void main(String[] args) {
        // write your code here

        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int K = in.nextInt();
        long ans = 0;

//        ans  = gcd(1,2);

        for (int i = 1; i <= K; i++) {
            for (int j = 1 ; j <=K; j++) {
                for (int k = 1; k <= K; k++) {
                    ans += gcd(i,j,k);
                }
            }
        }

        out.println(ans);

        in.close();
        out.close();
    }

    public static int gcd(int a, int b, int c){
        return gcd(gcd(a,b),c);
    }

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

    public static void main(String[] args) {
        // write your code here

        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int K = in.nextInt();
        long ans = 0;

//        ans  = gcd(1,2);

        for (int i = 1; i <= K; i++) {
            for (int j = 1 ; j <=K; j++) {
                for (int k = 1; k <= K; k++) {
                    ans += gcd(i,j,k);
                }
            }
        }

        out.println(ans);

        in.close();
        out.close();
    }

    public static int gcd(int a, int b, int c){
        return gcd(gcd(a,b),c);
    }

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

import java.io.PrintWriter;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // write your code here

        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int K = in.nextInt();
        long ans = 0;

//        ans  = gcd(1,2);

        for (int i = 1; i <= K; i++) {
            for (int j = 1 ; j <=K; j++) {
                for (int k = 1; k <= K; k++) {
                    ans += gcd(i,j,k);
                }
            }
        }

        out.println(ans);

        in.close();
        out.close();
    }

    public static int gcd(int a, int b, int c){
        return gcd(gcd(a,b),c);
    }

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