import java.util.*;
public class Main{
    private static int gcd(int m, int n) {
    if(m < n) return gcd(n, m);
    if(n == 0) return m;
    return gcd(n, m % n);
}
    public static void main(String args[]){
        Scanner sc= new Scanner(System.in);
        int n = sc.nextInt();
        int ans = 0;
        sc.close();
        for(int i=1;i<n+1;i++){
            for(int j=1;j<n+1;j++){
                for(int k=1;k<n+1;k++){
                    ans += gcd(gcd(i,j),k);
                }
            }
        }
        System.out.println(ans);
    }
} import java.util.*; util.* java public class Main{
    private static int gcd(int m, int n) {
    if(m < n) return gcd(n, m);
    if(n == 0) return m;
    return gcd(n, m % n);
}
    public static void main(String args[]){
        Scanner sc= new Scanner(System.in);
        int n = sc.nextInt();
        int ans = 0;
        sc.close();
        for(int i=1;i<n+1;i++){
            for(int j=1;j<n+1;j++){
                for(int k=1;k<n+1;k++){
                    ans += gcd(gcd(i,j),k);
                }
            }
        }
        System.out.println(ans);
    }
} Main private static int gcd(int m, int n) {
    if(m < n) return gcd(n, m);
    if(n == 0) return m;
    return gcd(n, m % n);
} gcd {
    if(m < n) return gcd(n, m);
    if(n == 0) return m;
    return gcd(n, m % n);
} if(m < n) return gcd(n, m); m < n m n return gcd(n, m); gcd(n, m) gcd n m if(n == 0) return m; n == 0 n 0 return m; m return gcd(n, m % n); gcd(n, m % n) gcd n m % n m n int m m int n n public static void main(String args[]){
        Scanner sc= new Scanner(System.in);
        int n = sc.nextInt();
        int ans = 0;
        sc.close();
        for(int i=1;i<n+1;i++){
            for(int j=1;j<n+1;j++){
                for(int k=1;k<n+1;k++){
                    ans += gcd(gcd(i,j),k);
                }
            }
        }
        System.out.println(ans);
    } main {
        Scanner sc= new Scanner(System.in);
        int n = sc.nextInt();
        int ans = 0;
        sc.close();
        for(int i=1;i<n+1;i++){
            for(int j=1;j<n+1;j++){
                for(int k=1;k<n+1;k++){
                    ans += gcd(gcd(i,j),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 int ans = 0; ans 0 sc.close() sc.close sc for(int i=1;i<n+1;i++){
            for(int j=1;j<n+1;j++){
                for(int k=1;k<n+1;k++){
                    ans += gcd(gcd(i,j),k);
                }
            }
        } int i=1; int i=1; i 1 i<n+1 i n+1 n 1 i++ i++ i {
            for(int j=1;j<n+1;j++){
                for(int k=1;k<n+1;k++){
                    ans += gcd(gcd(i,j),k);
                }
            }
        } {
            for(int j=1;j<n+1;j++){
                for(int k=1;k<n+1;k++){
                    ans += gcd(gcd(i,j),k);
                }
            }
        } for(int j=1;j<n+1;j++){
                for(int k=1;k<n+1;k++){
                    ans += gcd(gcd(i,j),k);
                }
            } int j=1; int j=1; j 1 j<n+1 j n+1 n 1 j++ j++ j {
                for(int k=1;k<n+1;k++){
                    ans += gcd(gcd(i,j),k);
                }
            } {
                for(int k=1;k<n+1;k++){
                    ans += gcd(gcd(i,j),k);
                }
            } for(int k=1;k<n+1;k++){
                    ans += gcd(gcd(i,j),k);
                } int k=1; int k=1; k 1 k<n+1 k n+1 n 1 k++ k++ k {
                    ans += gcd(gcd(i,j),k);
                } {
                    ans += gcd(gcd(i,j),k);
                } ans += gcd(gcd(i,j),k) ans gcd(gcd(i,j),k) gcd gcd(i,j) gcd i j k System.out.println(ans) System.out.println System.out System System.out ans String args[] args public class Main{
    private static int gcd(int m, int n) {
    if(m < n) return gcd(n, m);
    if(n == 0) return m;
    return gcd(n, m % n);
}
    public static void main(String args[]){
        Scanner sc= new Scanner(System.in);
        int n = sc.nextInt();
        int ans = 0;
        sc.close();
        for(int i=1;i<n+1;i++){
            for(int j=1;j<n+1;j++){
                for(int k=1;k<n+1;k++){
                    ans += gcd(gcd(i,j),k);
                }
            }
        }
        System.out.println(ans);
    }
} public class Main{
    private static int gcd(int m, int n) {
    if(m < n) return gcd(n, m);
    if(n == 0) return m;
    return gcd(n, m % n);
}
    public static void main(String args[]){
        Scanner sc= new Scanner(System.in);
        int n = sc.nextInt();
        int ans = 0;
        sc.close();
        for(int i=1;i<n+1;i++){
            for(int j=1;j<n+1;j++){
                for(int k=1;k<n+1;k++){
                    ans += gcd(gcd(i,j),k);
                }
            }
        }
        System.out.println(ans);
    }
} Main
import java.util.*;
public class Main{
    private static int gcd(int m, int n) {
    if(m < n) return gcd(n, m);
    if(n == 0) return m;
    return gcd(n, m % n);
}
    public static void main(String args[]){
        Scanner sc= new Scanner(System.in);
        int n = sc.nextInt();
        int ans = 0;
        sc.close();
        for(int i=1;i<n+1;i++){
            for(int j=1;j<n+1;j++){
                for(int k=1;k<n+1;k++){
                    ans += gcd(gcd(i,j),k);
                }
            }
        }
        System.out.println(ans);
    }
}