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


public class Main {
    private static int MOD = 1000000007;


    public static void main(String[] args) throws IOException {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String str = br.readLine();
        int k = Integer.parseInt(str);

        long total = 0;
        for (int i = 1; i <= k; i++) {
            for (int i1 = 1; i1 <= k; i1++) {
                for (int i2 = 1; i2 <= k; i2++) {
                    San s = new San(i, i1, i2);
                    int result = s.gcd();
                    total += result;
                }
            }
        }
        System.out.println(total);
    }


    static class San {
        public int a, b, c;

        public San(int a, int b, int c) {
            int[] is = {a, b, c};
            Arrays.sort(is);
            this.a = is[0];
            this.b = is[1];
            this.c = is[2];
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            San san = (San) o;
            return a == san.a &&
                    b == san.b &&
                    c == san.c;
        }

        @Override
        public int hashCode() {
            return Objects.hash(a, b, c);
        }

        public int gcd() {
            int g = Main.gcd(a, b);
            g = Main.gcd(g, c);
            return g;
        }
    }

    public static final int gcd(final int a, final int b) {
        return (b == 0) ? a : gcd(b, a % b);
    }
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io import java.util.*; util.* java public class Main {
    private static int MOD = 1000000007;


    public static void main(String[] args) throws IOException {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String str = br.readLine();
        int k = Integer.parseInt(str);

        long total = 0;
        for (int i = 1; i <= k; i++) {
            for (int i1 = 1; i1 <= k; i1++) {
                for (int i2 = 1; i2 <= k; i2++) {
                    San s = new San(i, i1, i2);
                    int result = s.gcd();
                    total += result;
                }
            }
        }
        System.out.println(total);
    }


    static class San {
        public int a, b, c;

        public San(int a, int b, int c) {
            int[] is = {a, b, c};
            Arrays.sort(is);
            this.a = is[0];
            this.b = is[1];
            this.c = is[2];
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            San san = (San) o;
            return a == san.a &&
                    b == san.b &&
                    c == san.c;
        }

        @Override
        public int hashCode() {
            return Objects.hash(a, b, c);
        }

        public int gcd() {
            int g = Main.gcd(a, b);
            g = Main.gcd(g, c);
            return g;
        }
    }

    public static final int gcd(final int a, final int b) {
        return (b == 0) ? a : gcd(b, a % b);
    }
} Main private static int MOD = 1000000007; MOD 1000000007 public static void main(String[] args) throws IOException {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String str = br.readLine();
        int k = Integer.parseInt(str);

        long total = 0;
        for (int i = 1; i <= k; i++) {
            for (int i1 = 1; i1 <= k; i1++) {
                for (int i2 = 1; i2 <= k; i2++) {
                    San s = new San(i, i1, i2);
                    int result = s.gcd();
                    total += result;
                }
            }
        }
        System.out.println(total);
    } main {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String str = br.readLine();
        int k = Integer.parseInt(str);

        long total = 0;
        for (int i = 1; i <= k; i++) {
            for (int i1 = 1; i1 <= k; i1++) {
                for (int i2 = 1; i2 <= k; i2++) {
                    San s = new San(i, i1, i2);
                    int result = s.gcd();
                    total += result;
                }
            }
        }
        System.out.println(total);
    } InputStreamReader isr = new InputStreamReader(System.in); isr new InputStreamReader(System.in) BufferedReader br = new BufferedReader(isr); br new BufferedReader(isr) String str = br.readLine(); str br.readLine() br.readLine br int k = Integer.parseInt(str); k Integer.parseInt(str) Integer.parseInt Integer str long total = 0; total 0 for (int i = 1; i <= k; i++) {
            for (int i1 = 1; i1 <= k; i1++) {
                for (int i2 = 1; i2 <= k; i2++) {
                    San s = new San(i, i1, i2);
                    int result = s.gcd();
                    total += result;
                }
            }
        } int i = 1; int i = 1; i 1 i <= k i k i++ i++ i {
            for (int i1 = 1; i1 <= k; i1++) {
                for (int i2 = 1; i2 <= k; i2++) {
                    San s = new San(i, i1, i2);
                    int result = s.gcd();
                    total += result;
                }
            }
        } {
            for (int i1 = 1; i1 <= k; i1++) {
                for (int i2 = 1; i2 <= k; i2++) {
                    San s = new San(i, i1, i2);
                    int result = s.gcd();
                    total += result;
                }
            }
        } for (int i1 = 1; i1 <= k; i1++) {
                for (int i2 = 1; i2 <= k; i2++) {
                    San s = new San(i, i1, i2);
                    int result = s.gcd();
                    total += result;
                }
            } int i1 = 1; int i1 = 1; i1 1 i1 <= k i1 k i1++ i1++ i1 {
                for (int i2 = 1; i2 <= k; i2++) {
                    San s = new San(i, i1, i2);
                    int result = s.gcd();
                    total += result;
                }
            } {
                for (int i2 = 1; i2 <= k; i2++) {
                    San s = new San(i, i1, i2);
                    int result = s.gcd();
                    total += result;
                }
            } for (int i2 = 1; i2 <= k; i2++) {
                    San s = new San(i, i1, i2);
                    int result = s.gcd();
                    total += result;
                } int i2 = 1; int i2 = 1; i2 1 i2 <= k i2 k i2++ i2++ i2 {
                    San s = new San(i, i1, i2);
                    int result = s.gcd();
                    total += result;
                } {
                    San s = new San(i, i1, i2);
                    int result = s.gcd();
                    total += result;
                } San s = new San(i, i1, i2); s new San(i, i1, i2) int result = s.gcd(); result s.gcd() s.gcd s total += result total result System.out.println(total) System.out.println System.out System System.out total String[] args args public static final int gcd(final int a, final int b) {
        return (b == 0) ? a : gcd(b, a % b);
    } gcd {
        return (b == 0) ? a : gcd(b, a % b);
    } return (b == 0) ? a : gcd(b, a % b); (b == 0) ? a : gcd(b, a % b) (b == 0) b 0 a gcd(b, a % b) gcd b a % b a b final int a a final int b b static class San {
        public int a, b, c;

        public San(int a, int b, int c) {
            int[] is = {a, b, c};
            Arrays.sort(is);
            this.a = is[0];
            this.b = is[1];
            this.c = is[2];
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            San san = (San) o;
            return a == san.a &&
                    b == san.b &&
                    c == san.c;
        }

        @Override
        public int hashCode() {
            return Objects.hash(a, b, c);
        }

        public int gcd() {
            int g = Main.gcd(a, b);
            g = Main.gcd(g, c);
            return g;
        }
    } San public int a a b b c; c public San(int a, int b, int c) {
            int[] is = {a, b, c};
            Arrays.sort(is);
            this.a = is[0];
            this.b = is[1];
            this.c = is[2];
        } San {
            int[] is = {a, b, c};
            Arrays.sort(is);
            this.a = is[0];
            this.b = is[1];
            this.c = is[2];
        } int[] is = {a, b, c}; is {a, b, c} a b c Arrays.sort(is) Arrays.sort Arrays is this.a = is[0] this.a this this.a is[0] is 0 this.b = is[1] this.b this this.b is[1] is 1 this.c = is[2] this.c this this.c is[2] is 2 int a a int b b int c c @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            San san = (San) o;
            return a == san.a &&
                    b == san.b &&
                    c == san.c;
        } equals {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            San san = (San) o;
            return a == san.a &&
                    b == san.b &&
                    c == san.c;
        } if (this == o) return true; this == o this o return true; true if (o == null || getClass() != o.getClass()) return false; o == null || getClass() != o.getClass() o == null o null getClass() != o.getClass() getClass() getClass o.getClass() o.getClass o return false; false San san = (San) o; san (San) o return a == san.a &&
                    b == san.b &&
                    c == san.c; a == san.a &&
                    b == san.b &&
                    c == san.c a == san.a &&
                    b == san.b &&
                    c == san.c a == san.a a san.a san san.a b == san.b b san.b san san.b c == san.c c san.c san san.c Object o o @Override
        public int hashCode() {
            return Objects.hash(a, b, c);
        } hashCode {
            return Objects.hash(a, b, c);
        } return Objects.hash(a, b, c); Objects.hash(a, b, c) Objects.hash Objects a b c public int gcd() {
            int g = Main.gcd(a, b);
            g = Main.gcd(g, c);
            return g;
        } gcd {
            int g = Main.gcd(a, b);
            g = Main.gcd(g, c);
            return g;
        } int g = Main.gcd(a, b); g Main.gcd(a, b) Main.gcd Main a b g = Main.gcd(g, c) g Main.gcd(g, c) Main.gcd Main g c return g; g public class Main {
    private static int MOD = 1000000007;


    public static void main(String[] args) throws IOException {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String str = br.readLine();
        int k = Integer.parseInt(str);

        long total = 0;
        for (int i = 1; i <= k; i++) {
            for (int i1 = 1; i1 <= k; i1++) {
                for (int i2 = 1; i2 <= k; i2++) {
                    San s = new San(i, i1, i2);
                    int result = s.gcd();
                    total += result;
                }
            }
        }
        System.out.println(total);
    }


    static class San {
        public int a, b, c;

        public San(int a, int b, int c) {
            int[] is = {a, b, c};
            Arrays.sort(is);
            this.a = is[0];
            this.b = is[1];
            this.c = is[2];
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            San san = (San) o;
            return a == san.a &&
                    b == san.b &&
                    c == san.c;
        }

        @Override
        public int hashCode() {
            return Objects.hash(a, b, c);
        }

        public int gcd() {
            int g = Main.gcd(a, b);
            g = Main.gcd(g, c);
            return g;
        }
    }

    public static final int gcd(final int a, final int b) {
        return (b == 0) ? a : gcd(b, a % b);
    }
} public class Main {
    private static int MOD = 1000000007;


    public static void main(String[] args) throws IOException {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String str = br.readLine();
        int k = Integer.parseInt(str);

        long total = 0;
        for (int i = 1; i <= k; i++) {
            for (int i1 = 1; i1 <= k; i1++) {
                for (int i2 = 1; i2 <= k; i2++) {
                    San s = new San(i, i1, i2);
                    int result = s.gcd();
                    total += result;
                }
            }
        }
        System.out.println(total);
    }


    static class San {
        public int a, b, c;

        public San(int a, int b, int c) {
            int[] is = {a, b, c};
            Arrays.sort(is);
            this.a = is[0];
            this.b = is[1];
            this.c = is[2];
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            San san = (San) o;
            return a == san.a &&
                    b == san.b &&
                    c == san.c;
        }

        @Override
        public int hashCode() {
            return Objects.hash(a, b, c);
        }

        public int gcd() {
            int g = Main.gcd(a, b);
            g = Main.gcd(g, c);
            return g;
        }
    }

    public static final int gcd(final int a, final int b) {
        return (b == 0) ? a : gcd(b, a % b);
    }
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;


public class Main {
    private static int MOD = 1000000007;


    public static void main(String[] args) throws IOException {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String str = br.readLine();
        int k = Integer.parseInt(str);

        long total = 0;
        for (int i = 1; i <= k; i++) {
            for (int i1 = 1; i1 <= k; i1++) {
                for (int i2 = 1; i2 <= k; i2++) {
                    San s = new San(i, i1, i2);
                    int result = s.gcd();
                    total += result;
                }
            }
        }
        System.out.println(total);
    }


    static class San {
        public int a, b, c;

        public San(int a, int b, int c) {
            int[] is = {a, b, c};
            Arrays.sort(is);
            this.a = is[0];
            this.b = is[1];
            this.c = is[2];
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            San san = (San) o;
            return a == san.a &&
                    b == san.b &&
                    c == san.c;
        }

        @Override
        public int hashCode() {
            return Objects.hash(a, b, c);
        }

        public int gcd() {
            int g = Main.gcd(a, b);
            g = Main.gcd(g, c);
            return g;
        }
    }

    public static final int gcd(final int a, final int b) {
        return (b == 0) ? a : gcd(b, a % b);
    }
}
