import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        int k = Integer.parseInt(f.readLine());
        long sum = 0;
        for(int i = 1; i <= k; i++){
            for(int j = 1; j <= k; j++){
                for(int jj = 1; jj <= k; jj++){
                    sum+=gcd(gcd(i, j), jj);
                }
            }
        }
        out.println(sum);
        out.close();
    }

    public static long gcd(long a, long b){
        while(b != 0){
            long t = b;
            b = a%b;
            a = t;
        }
        return a;
    }
} import java.util.*; util.* java import java.io.*; io.* java public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        int k = Integer.parseInt(f.readLine());
        long sum = 0;
        for(int i = 1; i <= k; i++){
            for(int j = 1; j <= k; j++){
                for(int jj = 1; jj <= k; jj++){
                    sum+=gcd(gcd(i, j), jj);
                }
            }
        }
        out.println(sum);
        out.close();
    }

    public static long gcd(long a, long b){
        while(b != 0){
            long t = b;
            b = a%b;
            a = t;
        }
        return a;
    }
} Main public static void main(String[] args) throws IOException{
        BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        int k = Integer.parseInt(f.readLine());
        long sum = 0;
        for(int i = 1; i <= k; i++){
            for(int j = 1; j <= k; j++){
                for(int jj = 1; jj <= k; jj++){
                    sum+=gcd(gcd(i, j), jj);
                }
            }
        }
        out.println(sum);
        out.close();
    } main {
        BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        int k = Integer.parseInt(f.readLine());
        long sum = 0;
        for(int i = 1; i <= k; i++){
            for(int j = 1; j <= k; j++){
                for(int jj = 1; jj <= k; jj++){
                    sum+=gcd(gcd(i, j), jj);
                }
            }
        }
        out.println(sum);
        out.close();
    } BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); f new BufferedReader(new InputStreamReader(System.in)) PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); out new PrintWriter(new OutputStreamWriter(System.out)) int k = Integer.parseInt(f.readLine()); k Integer.parseInt(f.readLine()) Integer.parseInt Integer f.readLine() f.readLine f long sum = 0; sum 0 for(int i = 1; i <= k; i++){
            for(int j = 1; j <= k; j++){
                for(int jj = 1; jj <= k; jj++){
                    sum+=gcd(gcd(i, j), jj);
                }
            }
        } int i = 1; int i = 1; i 1 i <= k i k i++ i++ i {
            for(int j = 1; j <= k; j++){
                for(int jj = 1; jj <= k; jj++){
                    sum+=gcd(gcd(i, j), jj);
                }
            }
        } {
            for(int j = 1; j <= k; j++){
                for(int jj = 1; jj <= k; jj++){
                    sum+=gcd(gcd(i, j), jj);
                }
            }
        } for(int j = 1; j <= k; j++){
                for(int jj = 1; jj <= k; jj++){
                    sum+=gcd(gcd(i, j), jj);
                }
            } int j = 1; int j = 1; j 1 j <= k j k j++ j++ j {
                for(int jj = 1; jj <= k; jj++){
                    sum+=gcd(gcd(i, j), jj);
                }
            } {
                for(int jj = 1; jj <= k; jj++){
                    sum+=gcd(gcd(i, j), jj);
                }
            } for(int jj = 1; jj <= k; jj++){
                    sum+=gcd(gcd(i, j), jj);
                } int jj = 1; int jj = 1; jj 1 jj <= k jj k jj++ jj++ jj {
                    sum+=gcd(gcd(i, j), jj);
                } {
                    sum+=gcd(gcd(i, j), jj);
                } sum+=gcd(gcd(i, j), jj) sum gcd(gcd(i, j), jj) gcd gcd(i, j) gcd i j jj out.println(sum) out.println out sum out.close() out.close out String[] args args public static long gcd(long a, long b){
        while(b != 0){
            long t = b;
            b = a%b;
            a = t;
        }
        return a;
    } gcd {
        while(b != 0){
            long t = b;
            b = a%b;
            a = t;
        }
        return a;
    } while(b != 0){
            long t = b;
            b = a%b;
            a = t;
        } b != 0 b 0 {
            long t = b;
            b = a%b;
            a = t;
        } long t = b; t b b = a%b b a%b a b a = t a t return a; a long a a long b b public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        int k = Integer.parseInt(f.readLine());
        long sum = 0;
        for(int i = 1; i <= k; i++){
            for(int j = 1; j <= k; j++){
                for(int jj = 1; jj <= k; jj++){
                    sum+=gcd(gcd(i, j), jj);
                }
            }
        }
        out.println(sum);
        out.close();
    }

    public static long gcd(long a, long b){
        while(b != 0){
            long t = b;
            b = a%b;
            a = t;
        }
        return a;
    }
} public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        int k = Integer.parseInt(f.readLine());
        long sum = 0;
        for(int i = 1; i <= k; i++){
            for(int j = 1; j <= k; j++){
                for(int jj = 1; jj <= k; jj++){
                    sum+=gcd(gcd(i, j), jj);
                }
            }
        }
        out.println(sum);
        out.close();
    }

    public static long gcd(long a, long b){
        while(b != 0){
            long t = b;
            b = a%b;
            a = t;
        }
        return a;
    }
} Main
import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        int k = Integer.parseInt(f.readLine());
        long sum = 0;
        for(int i = 1; i <= k; i++){
            for(int j = 1; j <= k; j++){
                for(int jj = 1; jj <= k; jj++){
                    sum+=gcd(gcd(i, j), jj);
                }
            }
        }
        out.println(sum);
        out.close();
    }

    public static long gcd(long a, long b){
        while(b != 0){
            long t = b;
            b = a%b;
            a = t;
        }
        return a;
    }
}
