import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner a =  new Scanner(System.in);
        int h = a.nextInt();
        int ans = 0;
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= h; j++){
                for(int k = 1; k <= h; k++){
                    int temp = gcd(i,j);
                    ans+=gcd(temp,k);
                }
            }
        }
        
        System.out.println(ans);
    }
    
    static int gcd(int x, int y){
        if(y == 0)return x;
        else{
            return gcd(y, x%y);
        }
    }
} import java.util.Scanner; Scanner java.util public class Main{
    public static void main(String[] args){
        Scanner a =  new Scanner(System.in);
        int h = a.nextInt();
        int ans = 0;
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= h; j++){
                for(int k = 1; k <= h; k++){
                    int temp = gcd(i,j);
                    ans+=gcd(temp,k);
                }
            }
        }
        
        System.out.println(ans);
    }
    
    static int gcd(int x, int y){
        if(y == 0)return x;
        else{
            return gcd(y, x%y);
        }
    }
} Main public static void main(String[] args){
        Scanner a =  new Scanner(System.in);
        int h = a.nextInt();
        int ans = 0;
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= h; j++){
                for(int k = 1; k <= h; k++){
                    int temp = gcd(i,j);
                    ans+=gcd(temp,k);
                }
            }
        }
        
        System.out.println(ans);
    } main {
        Scanner a =  new Scanner(System.in);
        int h = a.nextInt();
        int ans = 0;
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= h; j++){
                for(int k = 1; k <= h; k++){
                    int temp = gcd(i,j);
                    ans+=gcd(temp,k);
                }
            }
        }
        
        System.out.println(ans);
    } Scanner a =  new Scanner(System.in); a new Scanner(System.in) int h = a.nextInt(); h a.nextInt() a.nextInt a int ans = 0; ans 0 for(int i = 1; i <= h; i++){
            for(int j = 1; j <= h; j++){
                for(int k = 1; k <= h; k++){
                    int temp = gcd(i,j);
                    ans+=gcd(temp,k);
                }
            }
        } int i = 1; int i = 1; i 1 i <= h i h i++ i++ i {
            for(int j = 1; j <= h; j++){
                for(int k = 1; k <= h; k++){
                    int temp = gcd(i,j);
                    ans+=gcd(temp,k);
                }
            }
        } {
            for(int j = 1; j <= h; j++){
                for(int k = 1; k <= h; k++){
                    int temp = gcd(i,j);
                    ans+=gcd(temp,k);
                }
            }
        } for(int j = 1; j <= h; j++){
                for(int k = 1; k <= h; k++){
                    int temp = gcd(i,j);
                    ans+=gcd(temp,k);
                }
            } int j = 1; int j = 1; j 1 j <= h j h j++ j++ j {
                for(int k = 1; k <= h; k++){
                    int temp = gcd(i,j);
                    ans+=gcd(temp,k);
                }
            } {
                for(int k = 1; k <= h; k++){
                    int temp = gcd(i,j);
                    ans+=gcd(temp,k);
                }
            } for(int k = 1; k <= h; k++){
                    int temp = gcd(i,j);
                    ans+=gcd(temp,k);
                } int k = 1; int k = 1; k 1 k <= h k h k++ k++ k {
                    int temp = gcd(i,j);
                    ans+=gcd(temp,k);
                } {
                    int temp = gcd(i,j);
                    ans+=gcd(temp,k);
                } int temp = gcd(i,j); temp gcd(i,j) gcd i j ans+=gcd(temp,k) ans gcd(temp,k) gcd temp k System.out.println(ans) System.out.println System.out System System.out ans String[] args args static int gcd(int x, int y){
        if(y == 0)return x;
        else{
            return gcd(y, x%y);
        }
    } gcd {
        if(y == 0)return x;
        else{
            return gcd(y, x%y);
        }
    } if(y == 0)return x;
        else{
            return gcd(y, x%y);
        } y == 0 y 0 return x; x {
            return gcd(y, x%y);
        } return gcd(y, x%y); gcd(y, x%y) gcd y x%y x y int x x int y y public class Main{
    public static void main(String[] args){
        Scanner a =  new Scanner(System.in);
        int h = a.nextInt();
        int ans = 0;
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= h; j++){
                for(int k = 1; k <= h; k++){
                    int temp = gcd(i,j);
                    ans+=gcd(temp,k);
                }
            }
        }
        
        System.out.println(ans);
    }
    
    static int gcd(int x, int y){
        if(y == 0)return x;
        else{
            return gcd(y, x%y);
        }
    }
} public class Main{
    public static void main(String[] args){
        Scanner a =  new Scanner(System.in);
        int h = a.nextInt();
        int ans = 0;
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= h; j++){
                for(int k = 1; k <= h; k++){
                    int temp = gcd(i,j);
                    ans+=gcd(temp,k);
                }
            }
        }
        
        System.out.println(ans);
    }
    
    static int gcd(int x, int y){
        if(y == 0)return x;
        else{
            return gcd(y, x%y);
        }
    }
} Main
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner a =  new Scanner(System.in);
        int h = a.nextInt();
        int ans = 0;
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= h; j++){
                for(int k = 1; k <= h; k++){
                    int temp = gcd(i,j);
                    ans+=gcd(temp,k);
                }
            }
        }
        
        System.out.println(ans);
    }
    
    static int gcd(int x, int y){
        if(y == 0)return x;
        else{
            return gcd(y, x%y);
        }
    }
}