import java.util.Scanner;

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

        Scanner sc = new Scanner(System.in);
        int K = Integer.parseInt(sc.next());

        int sum=0;
        int parts = 0;
        int ansP =0;

        for(int k =1 ; k<K+1 ; k++){
            for(int i =1 ; i<K+1 ; i++){
                for(int j =1 ; j<K+1 ; j++){
                    parts= uclid(i,j);
                    ansP = uclid(k,parts);
                    sum = sum + ansP;
                }
            }
        }
        System.out.println(sum);
    }


    public static  int uclid(int m,int n) {
        int r;        
        r = m % n;    
        while ( r > 0 )  {  
            m = n; n = r;    
            r = m % n;
        }
        return n;
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String args[]){

        Scanner sc = new Scanner(System.in);
        int K = Integer.parseInt(sc.next());

        int sum=0;
        int parts = 0;
        int ansP =0;

        for(int k =1 ; k<K+1 ; k++){
            for(int i =1 ; i<K+1 ; i++){
                for(int j =1 ; j<K+1 ; j++){
                    parts= uclid(i,j);
                    ansP = uclid(k,parts);
                    sum = sum + ansP;
                }
            }
        }
        System.out.println(sum);
    }


    public static  int uclid(int m,int n) {
        int r;        
        r = m % n;    
        while ( r > 0 )  {  
            m = n; n = r;    
            r = m % n;
        }
        return n;
    }
} Main public static void main(String args[]){

        Scanner sc = new Scanner(System.in);
        int K = Integer.parseInt(sc.next());

        int sum=0;
        int parts = 0;
        int ansP =0;

        for(int k =1 ; k<K+1 ; k++){
            for(int i =1 ; i<K+1 ; i++){
                for(int j =1 ; j<K+1 ; j++){
                    parts= uclid(i,j);
                    ansP = uclid(k,parts);
                    sum = sum + ansP;
                }
            }
        }
        System.out.println(sum);
    } main {

        Scanner sc = new Scanner(System.in);
        int K = Integer.parseInt(sc.next());

        int sum=0;
        int parts = 0;
        int ansP =0;

        for(int k =1 ; k<K+1 ; k++){
            for(int i =1 ; i<K+1 ; i++){
                for(int j =1 ; j<K+1 ; j++){
                    parts= uclid(i,j);
                    ansP = uclid(k,parts);
                    sum = sum + ansP;
                }
            }
        }
        System.out.println(sum);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = Integer.parseInt(sc.next()); K Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int sum=0; sum 0 int parts = 0; parts 0 int ansP =0; ansP 0 for(int k =1 ; k<K+1 ; k++){
            for(int i =1 ; i<K+1 ; i++){
                for(int j =1 ; j<K+1 ; j++){
                    parts= uclid(i,j);
                    ansP = uclid(k,parts);
                    sum = sum + ansP;
                }
            }
        } int k =1 ; int k =1 ; k 1 k<K+1 k K+1 K 1 k++ k++ k {
            for(int i =1 ; i<K+1 ; i++){
                for(int j =1 ; j<K+1 ; j++){
                    parts= uclid(i,j);
                    ansP = uclid(k,parts);
                    sum = sum + ansP;
                }
            }
        } {
            for(int i =1 ; i<K+1 ; i++){
                for(int j =1 ; j<K+1 ; j++){
                    parts= uclid(i,j);
                    ansP = uclid(k,parts);
                    sum = sum + ansP;
                }
            }
        } for(int i =1 ; i<K+1 ; i++){
                for(int j =1 ; j<K+1 ; j++){
                    parts= uclid(i,j);
                    ansP = uclid(k,parts);
                    sum = sum + ansP;
                }
            } int i =1 ; int i =1 ; i 1 i<K+1 i K+1 K 1 i++ i++ i {
                for(int j =1 ; j<K+1 ; j++){
                    parts= uclid(i,j);
                    ansP = uclid(k,parts);
                    sum = sum + ansP;
                }
            } {
                for(int j =1 ; j<K+1 ; j++){
                    parts= uclid(i,j);
                    ansP = uclid(k,parts);
                    sum = sum + ansP;
                }
            } for(int j =1 ; j<K+1 ; j++){
                    parts= uclid(i,j);
                    ansP = uclid(k,parts);
                    sum = sum + ansP;
                } int j =1 ; int j =1 ; j 1 j<K+1 j K+1 K 1 j++ j++ j {
                    parts= uclid(i,j);
                    ansP = uclid(k,parts);
                    sum = sum + ansP;
                } {
                    parts= uclid(i,j);
                    ansP = uclid(k,parts);
                    sum = sum + ansP;
                } parts= uclid(i,j) parts uclid(i,j) uclid i j ansP = uclid(k,parts) ansP uclid(k,parts) uclid k parts sum = sum + ansP sum sum + ansP sum ansP System.out.println(sum) System.out.println System.out System System.out sum String args[] args public static  int uclid(int m,int n) {
        int r;        
        r = m % n;    
        while ( r > 0 )  {  
            m = n; n = r;    
            r = m % n;
        }
        return n;
    } uclid {
        int r;        
        r = m % n;    
        while ( r > 0 )  {  
            m = n; n = r;    
            r = m % n;
        }
        return n;
    } int r; r r = m % n r m % n m n while ( r > 0 )  {  
            m = n; n = r;    
            r = m % n;
        } r > 0 r 0 {  
            m = n; n = r;    
            r = m % n;
        } m = n m n n = r n r r = m % n r m % n m n return n; n int m m int n n public class Main {
    public static void main(String args[]){

        Scanner sc = new Scanner(System.in);
        int K = Integer.parseInt(sc.next());

        int sum=0;
        int parts = 0;
        int ansP =0;

        for(int k =1 ; k<K+1 ; k++){
            for(int i =1 ; i<K+1 ; i++){
                for(int j =1 ; j<K+1 ; j++){
                    parts= uclid(i,j);
                    ansP = uclid(k,parts);
                    sum = sum + ansP;
                }
            }
        }
        System.out.println(sum);
    }


    public static  int uclid(int m,int n) {
        int r;        
        r = m % n;    
        while ( r > 0 )  {  
            m = n; n = r;    
            r = m % n;
        }
        return n;
    }
} public class Main {
    public static void main(String args[]){

        Scanner sc = new Scanner(System.in);
        int K = Integer.parseInt(sc.next());

        int sum=0;
        int parts = 0;
        int ansP =0;

        for(int k =1 ; k<K+1 ; k++){
            for(int i =1 ; i<K+1 ; i++){
                for(int j =1 ; j<K+1 ; j++){
                    parts= uclid(i,j);
                    ansP = uclid(k,parts);
                    sum = sum + ansP;
                }
            }
        }
        System.out.println(sum);
    }


    public static  int uclid(int m,int n) {
        int r;        
        r = m % n;    
        while ( r > 0 )  {  
            m = n; n = r;    
            r = m % n;
        }
        return n;
    }
} Main
import java.util.Scanner;

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

        Scanner sc = new Scanner(System.in);
        int K = Integer.parseInt(sc.next());

        int sum=0;
        int parts = 0;
        int ansP =0;

        for(int k =1 ; k<K+1 ; k++){
            for(int i =1 ; i<K+1 ; i++){
                for(int j =1 ; j<K+1 ; j++){
                    parts= uclid(i,j);
                    ansP = uclid(k,parts);
                    sum = sum + ansP;
                }
            }
        }
        System.out.println(sum);
    }


    public static  int uclid(int m,int n) {
        int r;        
        r = m % n;    
        while ( r > 0 )  {  
            m = n; n = r;    
            r = m % n;
        }
        return n;
    }
}