import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        long sum= 0;

        for(int i =1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                for(int k = 1; k<=n; k++){
                    sum += getGCD(i,getGCD(j,k));
                }
            }
        }
        System.out.println(sum);
    }

    private static long getGCD(long a, long b){
        if(a%b == 0){
            return b;
        }
        else {
            return getGCD(b, a%b);
        }
    }
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        long sum= 0;

        for(int i =1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                for(int k = 1; k<=n; k++){
                    sum += getGCD(i,getGCD(j,k));
                }
            }
        }
        System.out.println(sum);
    }

    private static long getGCD(long a, long b){
        if(a%b == 0){
            return b;
        }
        else {
            return getGCD(b, a%b);
        }
    }
} Main public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        long sum= 0;

        for(int i =1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                for(int k = 1; k<=n; k++){
                    sum += getGCD(i,getGCD(j,k));
                }
            }
        }
        System.out.println(sum);
    } main {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        long sum= 0;

        for(int i =1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                for(int k = 1; k<=n; k++){
                    sum += getGCD(i,getGCD(j,k));
                }
            }
        }
        System.out.println(sum);
    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) int n = Integer.parseInt(br.readLine()); n Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br long sum= 0; sum 0 for(int i =1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                for(int k = 1; k<=n; k++){
                    sum += getGCD(i,getGCD(j,k));
                }
            }
        } 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++){
                    sum += getGCD(i,getGCD(j,k));
                }
            }
        } {
            for(int j = 1; j <= n; j++){
                for(int k = 1; k<=n; k++){
                    sum += getGCD(i,getGCD(j,k));
                }
            }
        } for(int j = 1; j <= n; j++){
                for(int k = 1; k<=n; k++){
                    sum += getGCD(i,getGCD(j,k));
                }
            } int j = 1; int j = 1; j 1 j <= n j n j++ j++ j {
                for(int k = 1; k<=n; k++){
                    sum += getGCD(i,getGCD(j,k));
                }
            } {
                for(int k = 1; k<=n; k++){
                    sum += getGCD(i,getGCD(j,k));
                }
            } for(int k = 1; k<=n; k++){
                    sum += getGCD(i,getGCD(j,k));
                } int k = 1; int k = 1; k 1 k<=n k n k++ k++ k {
                    sum += getGCD(i,getGCD(j,k));
                } {
                    sum += getGCD(i,getGCD(j,k));
                } sum += getGCD(i,getGCD(j,k)) sum getGCD(i,getGCD(j,k)) getGCD i getGCD(j,k) getGCD j k System.out.println(sum) System.out.println System.out System System.out sum String[] args args private static long getGCD(long a, long b){
        if(a%b == 0){
            return b;
        }
        else {
            return getGCD(b, a%b);
        }
    } getGCD {
        if(a%b == 0){
            return b;
        }
        else {
            return getGCD(b, a%b);
        }
    } if(a%b == 0){
            return b;
        }
        else {
            return getGCD(b, a%b);
        } a%b == 0 a%b a b 0 {
            return b;
        } return b; b {
            return getGCD(b, a%b);
        } return getGCD(b, a%b); getGCD(b, a%b) getGCD b a%b a b long a a long b b public class Main {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        long sum= 0;

        for(int i =1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                for(int k = 1; k<=n; k++){
                    sum += getGCD(i,getGCD(j,k));
                }
            }
        }
        System.out.println(sum);
    }

    private static long getGCD(long a, long b){
        if(a%b == 0){
            return b;
        }
        else {
            return getGCD(b, a%b);
        }
    }
} public class Main {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        long sum= 0;

        for(int i =1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                for(int k = 1; k<=n; k++){
                    sum += getGCD(i,getGCD(j,k));
                }
            }
        }
        System.out.println(sum);
    }

    private static long getGCD(long a, long b){
        if(a%b == 0){
            return b;
        }
        else {
            return getGCD(b, a%b);
        }
    }
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        long sum= 0;

        for(int i =1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                for(int k = 1; k<=n; k++){
                    sum += getGCD(i,getGCD(j,k));
                }
            }
        }
        System.out.println(sum);
    }

    private static long getGCD(long a, long b){
        if(a%b == 0){
            return b;
        }
        else {
            return getGCD(b, a%b);
        }
    }
}