import java.util.*;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int ans = 0;
        for(int a= 0;a<K;a++){
            for(int b= 0 ;b<K;b++){
                for(int c= 0;c<K;c++){
                    ans = ans + mgcd(a+1,b+1,c+1);
                }
            }
        }
        System.out.println(ans);
    }

    public static final int mgcd(int a, int b, int c) {
        int g = gcd(a,b);
        g = gcd(g,c);
    return g;
    }

    public static final int gcd(int a, int b) {

        if (a < b) {
            int tmp = a;
            a = b;
            b = tmp;
        }

        int r = -1;
        while (r != 0) {
            r = a % b;
            a = b;
            b = r;
        }

        return a;
    }


} import java.util.*; util.* java public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int ans = 0;
        for(int a= 0;a<K;a++){
            for(int b= 0 ;b<K;b++){
                for(int c= 0;c<K;c++){
                    ans = ans + mgcd(a+1,b+1,c+1);
                }
            }
        }
        System.out.println(ans);
    }

    public static final int mgcd(int a, int b, int c) {
        int g = gcd(a,b);
        g = gcd(g,c);
    return g;
    }

    public static final int gcd(int a, int b) {

        if (a < b) {
            int tmp = a;
            a = b;
            b = tmp;
        }

        int r = -1;
        while (r != 0) {
            r = a % b;
            a = b;
            b = r;
        }

        return a;
    }


} Main public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int ans = 0;
        for(int a= 0;a<K;a++){
            for(int b= 0 ;b<K;b++){
                for(int c= 0;c<K;c++){
                    ans = ans + mgcd(a+1,b+1,c+1);
                }
            }
        }
        System.out.println(ans);
    } main {

        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int ans = 0;
        for(int a= 0;a<K;a++){
            for(int b= 0 ;b<K;b++){
                for(int c= 0;c<K;c++){
                    ans = ans + mgcd(a+1,b+1,c+1);
                }
            }
        }
        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 int ans = 0; ans 0 for(int a= 0;a<K;a++){
            for(int b= 0 ;b<K;b++){
                for(int c= 0;c<K;c++){
                    ans = ans + mgcd(a+1,b+1,c+1);
                }
            }
        } int a= 0; int a= 0; a 0 a<K a K a++ a++ a {
            for(int b= 0 ;b<K;b++){
                for(int c= 0;c<K;c++){
                    ans = ans + mgcd(a+1,b+1,c+1);
                }
            }
        } {
            for(int b= 0 ;b<K;b++){
                for(int c= 0;c<K;c++){
                    ans = ans + mgcd(a+1,b+1,c+1);
                }
            }
        } for(int b= 0 ;b<K;b++){
                for(int c= 0;c<K;c++){
                    ans = ans + mgcd(a+1,b+1,c+1);
                }
            } int b= 0 ; int b= 0 ; b 0 b<K b K b++ b++ b {
                for(int c= 0;c<K;c++){
                    ans = ans + mgcd(a+1,b+1,c+1);
                }
            } {
                for(int c= 0;c<K;c++){
                    ans = ans + mgcd(a+1,b+1,c+1);
                }
            } for(int c= 0;c<K;c++){
                    ans = ans + mgcd(a+1,b+1,c+1);
                } int c= 0; int c= 0; c 0 c<K c K c++ c++ c {
                    ans = ans + mgcd(a+1,b+1,c+1);
                } {
                    ans = ans + mgcd(a+1,b+1,c+1);
                } ans = ans + mgcd(a+1,b+1,c+1) ans ans + mgcd(a+1,b+1,c+1) ans mgcd(a+1,b+1,c+1) mgcd a+1 a 1 b+1 b 1 c+1 c 1 System.out.println(ans) System.out.println System.out System System.out ans String[] args args public static final int mgcd(int a, int b, int c) {
        int g = gcd(a,b);
        g = gcd(g,c);
    return g;
    } mgcd {
        int g = gcd(a,b);
        g = gcd(g,c);
    return g;
    } int g = gcd(a,b); g gcd(a,b) gcd a b g = gcd(g,c) g gcd(g,c) gcd g c return g; g int a a int b b int c c public static final int gcd(int a, int b) {

        if (a < b) {
            int tmp = a;
            a = b;
            b = tmp;
        }

        int r = -1;
        while (r != 0) {
            r = a % b;
            a = b;
            b = r;
        }

        return a;
    } gcd {

        if (a < b) {
            int tmp = a;
            a = b;
            b = tmp;
        }

        int r = -1;
        while (r != 0) {
            r = a % b;
            a = b;
            b = r;
        }

        return a;
    } if (a < b) {
            int tmp = a;
            a = b;
            b = tmp;
        } a < b a b {
            int tmp = a;
            a = b;
            b = tmp;
        } int tmp = a; tmp a a = b a b b = tmp b tmp int r = -1; r -1 1 while (r != 0) {
            r = a % b;
            a = b;
            b = r;
        } r != 0 r 0 {
            r = a % b;
            a = b;
            b = r;
        } r = a % b r a % b a b a = b a b b = r b r return a; a int a a int b b public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int ans = 0;
        for(int a= 0;a<K;a++){
            for(int b= 0 ;b<K;b++){
                for(int c= 0;c<K;c++){
                    ans = ans + mgcd(a+1,b+1,c+1);
                }
            }
        }
        System.out.println(ans);
    }

    public static final int mgcd(int a, int b, int c) {
        int g = gcd(a,b);
        g = gcd(g,c);
    return g;
    }

    public static final int gcd(int a, int b) {

        if (a < b) {
            int tmp = a;
            a = b;
            b = tmp;
        }

        int r = -1;
        while (r != 0) {
            r = a % b;
            a = b;
            b = r;
        }

        return a;
    }


} public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int ans = 0;
        for(int a= 0;a<K;a++){
            for(int b= 0 ;b<K;b++){
                for(int c= 0;c<K;c++){
                    ans = ans + mgcd(a+1,b+1,c+1);
                }
            }
        }
        System.out.println(ans);
    }

    public static final int mgcd(int a, int b, int c) {
        int g = gcd(a,b);
        g = gcd(g,c);
    return g;
    }

    public static final int gcd(int a, int b) {

        if (a < b) {
            int tmp = a;
            a = b;
            b = tmp;
        }

        int r = -1;
        while (r != 0) {
            r = a % b;
            a = b;
            b = r;
        }

        return a;
    }


} Main
import java.util.*;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int ans = 0;
        for(int a= 0;a<K;a++){
            for(int b= 0 ;b<K;b++){
                for(int c= 0;c<K;c++){
                    ans = ans + mgcd(a+1,b+1,c+1);
                }
            }
        }
        System.out.println(ans);
    }

    public static final int mgcd(int a, int b, int c) {
        int g = gcd(a,b);
        g = gcd(g,c);
    return g;
    }

    public static final int gcd(int a, int b) {

        if (a < b) {
            int tmp = a;
            a = b;
            b = tmp;
        }

        int r = -1;
        while (r != 0) {
            r = a % b;
            a = b;
            b = r;
        }

        return a;
    }


}