import java.util.*;

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.run();
    }
    
    // 変数一覧
    int k;

    public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        k = sc.nextInt();
        sc.close();

        // 解答処理
        solve();
    }
    
    private void solve() {
        int ans = 0;
        for (int i = 1; i <= k; i++) {
            for (int j = 1; j <= k; j++) {
                for (int l = 1; l <= k; l++) {
                    ans += gcd(i, gcd(j, l));
                }
            }    
        }
        System.out.println(ans);
    }
    
    private int gcd(int a, int b) {
        int r = 0;
        if (a < b) {
            int tmp;
            tmp = a;
            a = b;
            b = tmp;
        }
        r = a % b;
        while (r != 0) {
            a = b;
            b = r;
            r = a % b;
        }
        return b;
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.run();
    }
    
    // 変数一覧
    int k;

    public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        k = sc.nextInt();
        sc.close();

        // 解答処理
        solve();
    }
    
    private void solve() {
        int ans = 0;
        for (int i = 1; i <= k; i++) {
            for (int j = 1; j <= k; j++) {
                for (int l = 1; l <= k; l++) {
                    ans += gcd(i, gcd(j, l));
                }
            }    
        }
        System.out.println(ans);
    }
    
    private int gcd(int a, int b) {
        int r = 0;
        if (a < b) {
            int tmp;
            tmp = a;
            a = b;
            b = tmp;
        }
        r = a % b;
        while (r != 0) {
            a = b;
            b = r;
            r = a % b;
        }
        return b;
    }
} Main // 変数一覧
    int k; k public static void main(String[] args) {
        Main main = new Main();
        main.run();
    } main {
        Main main = new Main();
        main.run();
    } Main main = new Main(); main new Main() main.run() main.run main String[] args args public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        k = sc.nextInt();
        sc.close();

        // 解答処理
        solve();
    } run {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        k = sc.nextInt();
        sc.close();

        // 解答処理
        solve();
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) k = sc.nextInt() k sc.nextInt() sc.nextInt sc sc.close() sc.close sc solve() solve private void solve() {
        int ans = 0;
        for (int i = 1; i <= k; i++) {
            for (int j = 1; j <= k; j++) {
                for (int l = 1; l <= k; l++) {
                    ans += gcd(i, gcd(j, l));
                }
            }    
        }
        System.out.println(ans);
    } solve {
        int ans = 0;
        for (int i = 1; i <= k; i++) {
            for (int j = 1; j <= k; j++) {
                for (int l = 1; l <= k; l++) {
                    ans += gcd(i, gcd(j, l));
                }
            }    
        }
        System.out.println(ans);
    } int ans = 0; ans 0 for (int i = 1; i <= k; i++) {
            for (int j = 1; j <= k; j++) {
                for (int l = 1; l <= k; l++) {
                    ans += gcd(i, gcd(j, l));
                }
            }    
        } int i = 1; int i = 1; i 1 i <= k i k i++ i++ i {
            for (int j = 1; j <= k; j++) {
                for (int l = 1; l <= k; l++) {
                    ans += gcd(i, gcd(j, l));
                }
            }    
        } {
            for (int j = 1; j <= k; j++) {
                for (int l = 1; l <= k; l++) {
                    ans += gcd(i, gcd(j, l));
                }
            }    
        } for (int j = 1; j <= k; j++) {
                for (int l = 1; l <= k; l++) {
                    ans += gcd(i, gcd(j, l));
                }
            } int j = 1; int j = 1; j 1 j <= k j k j++ j++ j {
                for (int l = 1; l <= k; l++) {
                    ans += gcd(i, gcd(j, l));
                }
            } {
                for (int l = 1; l <= k; l++) {
                    ans += gcd(i, gcd(j, l));
                }
            } for (int l = 1; l <= k; l++) {
                    ans += gcd(i, gcd(j, l));
                } int l = 1; int l = 1; l 1 l <= k l k l++ l++ l {
                    ans += gcd(i, gcd(j, l));
                } {
                    ans += gcd(i, gcd(j, l));
                } ans += gcd(i, gcd(j, l)) ans gcd(i, gcd(j, l)) gcd i gcd(j, l) gcd j l System.out.println(ans) System.out.println System.out System System.out ans private int gcd(int a, int b) {
        int r = 0;
        if (a < b) {
            int tmp;
            tmp = a;
            a = b;
            b = tmp;
        }
        r = a % b;
        while (r != 0) {
            a = b;
            b = r;
            r = a % b;
        }
        return b;
    } gcd {
        int r = 0;
        if (a < b) {
            int tmp;
            tmp = a;
            a = b;
            b = tmp;
        }
        r = a % b;
        while (r != 0) {
            a = b;
            b = r;
            r = a % b;
        }
        return b;
    } int r = 0; r 0 if (a < b) {
            int tmp;
            tmp = a;
            a = b;
            b = tmp;
        } a < b a b {
            int tmp;
            tmp = a;
            a = b;
            b = tmp;
        } int tmp; tmp tmp = a tmp a a = b a b b = tmp b tmp r = a % b r a % b a b while (r != 0) {
            a = b;
            b = r;
            r = a % b;
        } r != 0 r 0 {
            a = b;
            b = r;
            r = a % b;
        } a = b a b b = r b r r = a % b r a % b a b return b; b int a a int b b public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.run();
    }
    
    // 変数一覧
    int k;

    public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        k = sc.nextInt();
        sc.close();

        // 解答処理
        solve();
    }
    
    private void solve() {
        int ans = 0;
        for (int i = 1; i <= k; i++) {
            for (int j = 1; j <= k; j++) {
                for (int l = 1; l <= k; l++) {
                    ans += gcd(i, gcd(j, l));
                }
            }    
        }
        System.out.println(ans);
    }
    
    private int gcd(int a, int b) {
        int r = 0;
        if (a < b) {
            int tmp;
            tmp = a;
            a = b;
            b = tmp;
        }
        r = a % b;
        while (r != 0) {
            a = b;
            b = r;
            r = a % b;
        }
        return b;
    }
} public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.run();
    }
    
    // 変数一覧
    int k;

    public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        k = sc.nextInt();
        sc.close();

        // 解答処理
        solve();
    }
    
    private void solve() {
        int ans = 0;
        for (int i = 1; i <= k; i++) {
            for (int j = 1; j <= k; j++) {
                for (int l = 1; l <= k; l++) {
                    ans += gcd(i, gcd(j, l));
                }
            }    
        }
        System.out.println(ans);
    }
    
    private int gcd(int a, int b) {
        int r = 0;
        if (a < b) {
            int tmp;
            tmp = a;
            a = b;
            b = tmp;
        }
        r = a % b;
        while (r != 0) {
            a = b;
            b = r;
            r = a % b;
        }
        return b;
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.run();
    }
    
    // 変数一覧
    int k;

    public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        k = sc.nextInt();
        sc.close();

        // 解答処理
        solve();
    }
    
    private void solve() {
        int ans = 0;
        for (int i = 1; i <= k; i++) {
            for (int j = 1; j <= k; j++) {
                for (int l = 1; l <= k; l++) {
                    ans += gcd(i, gcd(j, l));
                }
            }    
        }
        System.out.println(ans);
    }
    
    private int gcd(int a, int b) {
        int r = 0;
        if (a < b) {
            int tmp;
            tmp = a;
            a = b;
            b = tmp;
        }
        r = a % b;
        while (r != 0) {
            a = b;
            b = r;
            r = a % b;
        }
        return b;
    }
}