

import java.util.Scanner;

public class Main {
    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);
    }

    private static int euclid(int m, int n) {
        int tmp;

        if(m < n) {
            tmp = m;
            m = n;
            n = tmp;
        }
        while(n > 0){
            tmp = m % n;
            m = n;
            n = tmp;
        }
        return m;
    }
} import java.util.Scanner; Scanner java.util public class Main {
    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);
    }

    private static int euclid(int m, int n) {
        int tmp;

        if(m < n) {
            tmp = m;
            m = n;
            n = tmp;
        }
        while(n > 0){
            tmp = m % n;
            m = n;
            n = tmp;
        }
        return m;
    }
} Main 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 private static int euclid(int m, int n) {
        int tmp;

        if(m < n) {
            tmp = m;
            m = n;
            n = tmp;
        }
        while(n > 0){
            tmp = m % n;
            m = n;
            n = tmp;
        }
        return m;
    } euclid {
        int tmp;

        if(m < n) {
            tmp = m;
            m = n;
            n = tmp;
        }
        while(n > 0){
            tmp = m % n;
            m = n;
            n = tmp;
        }
        return m;
    } int tmp; tmp if(m < n) {
            tmp = m;
            m = n;
            n = tmp;
        } m < n m n {
            tmp = m;
            m = n;
            n = tmp;
        } tmp = m tmp m m = n m n n = tmp n tmp while(n > 0){
            tmp = m % n;
            m = n;
            n = tmp;
        } n > 0 n 0 {
            tmp = m % n;
            m = n;
            n = tmp;
        } tmp = m % n tmp m % n m n m = n m n n = tmp n tmp return m; m int m m int n n public class Main {
    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);
    }

    private static int euclid(int m, int n) {
        int tmp;

        if(m < n) {
            tmp = m;
            m = n;
            n = tmp;
        }
        while(n > 0){
            tmp = m % n;
            m = n;
            n = tmp;
        }
        return m;
    }
} public class Main {
    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);
    }

    private static int euclid(int m, int n) {
        int tmp;

        if(m < n) {
            tmp = m;
            m = n;
            n = tmp;
        }
        while(n > 0){
            tmp = m % n;
            m = n;
            n = tmp;
        }
        return m;
    }
} Main


import java.util.Scanner;

public class Main {
    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);
    }

    private static int euclid(int m, int n) {
        int tmp;

        if(m < n) {
            tmp = m;
            m = n;
            n = tmp;
        }
        while(n > 0){
            tmp = m % n;
            m = n;
            n = tmp;
        }
        return m;
    }
}
