import java.util.*;
import java.util.Arrays;
public class Main{
    private static int euclid(int m, int n) {
        if(n==0){
            return m;
        }
        else{
            return euclid(n, m%n);
        }
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int result = 0;
 
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                for(int k = 1; k <= n; k++) {
                    result += euclid(j,euclid(k,i));
                }
            }
        }
        System.out.println(result);
    


    }
} import java.util.*; util.* java import java.util.Arrays; Arrays java.util public class Main{
    private static int euclid(int m, int n) {
        if(n==0){
            return m;
        }
        else{
            return euclid(n, m%n);
        }
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int result = 0;
 
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                for(int k = 1; k <= n; k++) {
                    result += euclid(j,euclid(k,i));
                }
            }
        }
        System.out.println(result);
    


    }
} Main private static int euclid(int m, int n) {
        if(n==0){
            return m;
        }
        else{
            return euclid(n, m%n);
        }
    } euclid {
        if(n==0){
            return m;
        }
        else{
            return euclid(n, m%n);
        }
    } if(n==0){
            return m;
        }
        else{
            return euclid(n, m%n);
        } n==0 n 0 {
            return m;
        } return m; m {
            return euclid(n, m%n);
        } return euclid(n, m%n); euclid(n, m%n) euclid 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 result = 0;
 
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                for(int k = 1; k <= n; k++) {
                    result += euclid(j,euclid(k,i));
                }
            }
        }
        System.out.println(result);
    


    } main {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int result = 0;
 
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                for(int k = 1; k <= n; k++) {
                    result += euclid(j,euclid(k,i));
                }
            }
        }
        System.out.println(result);
    


    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int result = 0; result 0 for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                for(int k = 1; k <= n; k++) {
                    result += euclid(j,euclid(k,i));
                }
            }
        } int i = 1; int i = 1; i 1 i <= n i n i++ i++ i {
            for(int j = 1; j <= n; j++) {
                for(int k = 1; k <= n; k++) {
                    result += euclid(j,euclid(k,i));
                }
            }
        } {
            for(int j = 1; j <= n; j++) {
                for(int k = 1; k <= n; k++) {
                    result += euclid(j,euclid(k,i));
                }
            }
        } for(int j = 1; j <= n; j++) {
                for(int k = 1; k <= n; k++) {
                    result += euclid(j,euclid(k,i));
                }
            } int j = 1; int j = 1; j 1 j <= n j n j++ j++ j {
                for(int k = 1; k <= n; k++) {
                    result += euclid(j,euclid(k,i));
                }
            } {
                for(int k = 1; k <= n; k++) {
                    result += euclid(j,euclid(k,i));
                }
            } for(int k = 1; k <= n; k++) {
                    result += euclid(j,euclid(k,i));
                } int k = 1; int k = 1; k 1 k <= n k n k++ k++ k {
                    result += euclid(j,euclid(k,i));
                } {
                    result += euclid(j,euclid(k,i));
                } result += euclid(j,euclid(k,i)) result euclid(j,euclid(k,i)) euclid j euclid(k,i) euclid k i System.out.println(result) System.out.println System.out System System.out result String[] args args public class Main{
    private static int euclid(int m, int n) {
        if(n==0){
            return m;
        }
        else{
            return euclid(n, m%n);
        }
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int result = 0;
 
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                for(int k = 1; k <= n; k++) {
                    result += euclid(j,euclid(k,i));
                }
            }
        }
        System.out.println(result);
    


    }
} public class Main{
    private static int euclid(int m, int n) {
        if(n==0){
            return m;
        }
        else{
            return euclid(n, m%n);
        }
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int result = 0;
 
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                for(int k = 1; k <= n; k++) {
                    result += euclid(j,euclid(k,i));
                }
            }
        }
        System.out.println(result);
    


    }
} Main
import java.util.*;
import java.util.Arrays;
public class Main{
    private static int euclid(int m, int n) {
        if(n==0){
            return m;
        }
        else{
            return euclid(n, m%n);
        }
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int result = 0;
 
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                for(int k = 1; k <= n; k++) {
                    result += euclid(j,euclid(k,i));
                }
            }
        }
        System.out.println(result);
    


    }
}