
import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        solve(System.in, System.out);
    }

    static void solve(InputStream is, PrintStream os) {
        // Your code here!
        Scanner scan = new Scanner(is);
        long K = scan.nextLong();

        long ans = 0;

        for(int i = 1; i <= K; i++) {
            for(int j = 1; j <= K; j++) {
                long gcd1 = gcd(i, j);
                for(int k = 1; k <= K; k++) {
                    long gcd2 = gcd(gcd1, k);
                    ans += gcd2;
                }
            }
        }

        os.println(ans);

    }

    static public long gcd(long a, long b) {
        if(a > b) {
            long tmp = a;
            a = b;
            b = tmp;
        }
        while(a > 0) {
            long tmpa = a;
            a = b % a;
            b = tmpa;
        }
        return b;
    }
} import java.io.InputStream; InputStream java.io import java.io.PrintStream; PrintStream java.io import java.util.ArrayList; ArrayList java.util import java.util.Arrays; Arrays java.util import java.util.HashMap; HashMap java.util import java.util.List; List java.util import java.util.Map; Map java.util import java.util.PriorityQueue; PriorityQueue java.util import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) throws Exception {
        solve(System.in, System.out);
    }

    static void solve(InputStream is, PrintStream os) {
        // Your code here!
        Scanner scan = new Scanner(is);
        long K = scan.nextLong();

        long ans = 0;

        for(int i = 1; i <= K; i++) {
            for(int j = 1; j <= K; j++) {
                long gcd1 = gcd(i, j);
                for(int k = 1; k <= K; k++) {
                    long gcd2 = gcd(gcd1, k);
                    ans += gcd2;
                }
            }
        }

        os.println(ans);

    }

    static public long gcd(long a, long b) {
        if(a > b) {
            long tmp = a;
            a = b;
            b = tmp;
        }
        while(a > 0) {
            long tmpa = a;
            a = b % a;
            b = tmpa;
        }
        return b;
    }
} Main public static void main(String[] args) throws Exception {
        solve(System.in, System.out);
    } main {
        solve(System.in, System.out);
    } solve(System.in, System.out) solve System.in System System.in System.out System System.out String[] args args static void solve(InputStream is, PrintStream os) {
        // Your code here!
        Scanner scan = new Scanner(is);
        long K = scan.nextLong();

        long ans = 0;

        for(int i = 1; i <= K; i++) {
            for(int j = 1; j <= K; j++) {
                long gcd1 = gcd(i, j);
                for(int k = 1; k <= K; k++) {
                    long gcd2 = gcd(gcd1, k);
                    ans += gcd2;
                }
            }
        }

        os.println(ans);

    } solve {
        // Your code here!
        Scanner scan = new Scanner(is);
        long K = scan.nextLong();

        long ans = 0;

        for(int i = 1; i <= K; i++) {
            for(int j = 1; j <= K; j++) {
                long gcd1 = gcd(i, j);
                for(int k = 1; k <= K; k++) {
                    long gcd2 = gcd(gcd1, k);
                    ans += gcd2;
                }
            }
        }

        os.println(ans);

    } Scanner scan = new Scanner(is); scan new Scanner(is) long K = scan.nextLong(); K scan.nextLong() scan.nextLong scan long ans = 0; ans 0 for(int i = 1; i <= K; i++) {
            for(int j = 1; j <= K; j++) {
                long gcd1 = gcd(i, j);
                for(int k = 1; k <= K; k++) {
                    long gcd2 = gcd(gcd1, k);
                    ans += gcd2;
                }
            }
        } int i = 1; int i = 1; i 1 i <= K i K i++ i++ i {
            for(int j = 1; j <= K; j++) {
                long gcd1 = gcd(i, j);
                for(int k = 1; k <= K; k++) {
                    long gcd2 = gcd(gcd1, k);
                    ans += gcd2;
                }
            }
        } {
            for(int j = 1; j <= K; j++) {
                long gcd1 = gcd(i, j);
                for(int k = 1; k <= K; k++) {
                    long gcd2 = gcd(gcd1, k);
                    ans += gcd2;
                }
            }
        } for(int j = 1; j <= K; j++) {
                long gcd1 = gcd(i, j);
                for(int k = 1; k <= K; k++) {
                    long gcd2 = gcd(gcd1, k);
                    ans += gcd2;
                }
            } int j = 1; int j = 1; j 1 j <= K j K j++ j++ j {
                long gcd1 = gcd(i, j);
                for(int k = 1; k <= K; k++) {
                    long gcd2 = gcd(gcd1, k);
                    ans += gcd2;
                }
            } {
                long gcd1 = gcd(i, j);
                for(int k = 1; k <= K; k++) {
                    long gcd2 = gcd(gcd1, k);
                    ans += gcd2;
                }
            } long gcd1 = gcd(i, j); gcd1 gcd(i, j) gcd i j for(int k = 1; k <= K; k++) {
                    long gcd2 = gcd(gcd1, k);
                    ans += gcd2;
                } int k = 1; int k = 1; k 1 k <= K k K k++ k++ k {
                    long gcd2 = gcd(gcd1, k);
                    ans += gcd2;
                } {
                    long gcd2 = gcd(gcd1, k);
                    ans += gcd2;
                } long gcd2 = gcd(gcd1, k); gcd2 gcd(gcd1, k) gcd gcd1 k ans += gcd2 ans gcd2 os.println(ans) os.println os ans InputStream is is PrintStream os os static public long gcd(long a, long b) {
        if(a > b) {
            long tmp = a;
            a = b;
            b = tmp;
        }
        while(a > 0) {
            long tmpa = a;
            a = b % a;
            b = tmpa;
        }
        return b;
    } gcd {
        if(a > b) {
            long tmp = a;
            a = b;
            b = tmp;
        }
        while(a > 0) {
            long tmpa = a;
            a = b % a;
            b = tmpa;
        }
        return b;
    } if(a > b) {
            long tmp = a;
            a = b;
            b = tmp;
        } a > b a b {
            long tmp = a;
            a = b;
            b = tmp;
        } long tmp = a; tmp a a = b a b b = tmp b tmp while(a > 0) {
            long tmpa = a;
            a = b % a;
            b = tmpa;
        } a > 0 a 0 {
            long tmpa = a;
            a = b % a;
            b = tmpa;
        } long tmpa = a; tmpa a a = b % a a b % a b a b = tmpa b tmpa return b; b long a a long b b public class Main {
    public static void main(String[] args) throws Exception {
        solve(System.in, System.out);
    }

    static void solve(InputStream is, PrintStream os) {
        // Your code here!
        Scanner scan = new Scanner(is);
        long K = scan.nextLong();

        long ans = 0;

        for(int i = 1; i <= K; i++) {
            for(int j = 1; j <= K; j++) {
                long gcd1 = gcd(i, j);
                for(int k = 1; k <= K; k++) {
                    long gcd2 = gcd(gcd1, k);
                    ans += gcd2;
                }
            }
        }

        os.println(ans);

    }

    static public long gcd(long a, long b) {
        if(a > b) {
            long tmp = a;
            a = b;
            b = tmp;
        }
        while(a > 0) {
            long tmpa = a;
            a = b % a;
            b = tmpa;
        }
        return b;
    }
} public class Main {
    public static void main(String[] args) throws Exception {
        solve(System.in, System.out);
    }

    static void solve(InputStream is, PrintStream os) {
        // Your code here!
        Scanner scan = new Scanner(is);
        long K = scan.nextLong();

        long ans = 0;

        for(int i = 1; i <= K; i++) {
            for(int j = 1; j <= K; j++) {
                long gcd1 = gcd(i, j);
                for(int k = 1; k <= K; k++) {
                    long gcd2 = gcd(gcd1, k);
                    ans += gcd2;
                }
            }
        }

        os.println(ans);

    }

    static public long gcd(long a, long b) {
        if(a > b) {
            long tmp = a;
            a = b;
            b = tmp;
        }
        while(a > 0) {
            long tmpa = a;
            a = b % a;
            b = tmpa;
        }
        return b;
    }
} Main

import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        solve(System.in, System.out);
    }

    static void solve(InputStream is, PrintStream os) {
        // Your code here!
        Scanner scan = new Scanner(is);
        long K = scan.nextLong();

        long ans = 0;

        for(int i = 1; i <= K; i++) {
            for(int j = 1; j <= K; j++) {
                long gcd1 = gcd(i, j);
                for(int k = 1; k <= K; k++) {
                    long gcd2 = gcd(gcd1, k);
                    ans += gcd2;
                }
            }
        }

        os.println(ans);

    }

    static public long gcd(long a, long b) {
        if(a > b) {
            long tmp = a;
            a = b;
            b = tmp;
        }
        while(a > 0) {
            long tmpa = a;
            a = b % a;
            b = tmpa;
        }
        return b;
    }
}